Professional Documents
Culture Documents
Object Oriented Programming
Object Oriented Programming
Sohail Basheer
Lecturer- Computer Science (visiting)
Department of Computer Science & IT (Evening Program)
Federal Urdu University Arts, Science & Technology
Gulshan Campus Karachi
OOP is widely accepted as being far more flexible than other computer programming languages. OOPs
use three basic concepts as the fundamentals for the programming language: classes, objects and
methods. Additionally, Inheritance, Abstraction, Polymorphism, Event Handling and Encapsulation.
2) What is a Class?
Class is a template for a set of objects that share a common structure and a common behavior.
3) What is an Object?
Object is an instance of a class. It has state, behavior and identity. It is also called as an instance of a
class.
4) What is an Instance?
An instance has state, behavior and identity. The structure and behavior of similar classes are defined
in their common class. An instance is also called as an object.
Inheritance is a relationship among classes, wherein one class shares the structure or behavior defined 1
in another class. This is called Single Inheritance. If a class shares the structure or behavior from multiple
classes, then it is called Multiple Inheritance. Inheritance defines is-a hierarchy among classes in which
one subclass inherits from one or more generalized superclasses.
9) What is meant by Polymorphism?
Polymorphism literally means taking more than one form. Polymorphism is a characteristic of being
able to assign a different behavior or value in a subclass, to something that was declared in a parent
class.
2
23) In Java, How to make an object completely encapsulated?
All the instance variables should be declared as private and public getter and setter methods should
be provided for accessing the instance variables.
What is a class?
Class is concrete representation of an entity. It represents a group of objects, which hold similar attributes
and behavior. It provides Abstraction and Encapsulations.
What is an Object? What is Object Oriented Programming?
Object represents/resembles a Physical/real entity. An object is simply something you can give a name.
Object Oriented Programming is a Style of programming that represents a program as a system of
objects and enables code-reuse.
What is Encapsulation?
Encapsulation is binding of attributes and behaviors. Hiding the actual implementation and exposing the
functionality of any object. Encapsulation is the first step towards OOPS, is the procedure of covering up
of data and functions into a single unit (called class). Its main aim is to protect the data from out side
world
What is Abstraction?
Hiding the complexity. It is a process of defining communication interface for the functionality and hiding
rest of the things.
What is Overloading?
Adding a new method with the same name in same/derived class but with different number/types of
parameters. It implements Polymorphism.
What is Overriding
A process of creating different implementation of a method having a same name as base class, in a
derived class. It implements Inheritance.
What is Shadowing?
When the method is defined as Final/sealed in base class and not override able and we need to provide
different implementation for the same. This process is known as shadowing, uses shadows/new keyword.
What is Inheritance?
It is a process of acquiring attributes and behaviors from another object (normally a class or interface).
What is an Abstract class?
An abstract class is a special kind of class that cannot be instantiated. It normally contains one or more
abstract methods or abstract properties. It provides body to a class.
3
What is an Interface?
An interface has no implementation; it only has the signature or in other words, just the definition of the
methods without the body.
What is Polymorphism?
Mean by more than one form. Ability to provide different implementation based on different number/type
of parameters.
What is Pure-Polymorphism?
When a method is declared as abstract/virtual method in a base class and which is overridden in a base
class. If we create a variable of a type of a base class and assign an object of a derived class to it, it will
be decided at a run time, which implementation of a method is to be called. This is known as Pure-
Polymorphism or Late-Binding.
What is a Constructor?
A special function Always called whenever an instance of the class is created.
class Test
{
int a,b;
Test()
{
a=9;
b=8;
}
};
4
Test t2(t1); // calling Test copy constructor
Test t2 = t1;// calling Test copy constructor
Copy constructors are called in following cases:
when a function returns an object of that class by value
when the object of that class is passed by value as an argument to a function
when you construct an object based on another object of the same class
When compiler generates a temporary object
6
What is a scope resolution operator?
A scope resolution operator (::), can be used to define the member functions of a class outside the class.
What do you mean by pure virtual functions?
A pure virtual member function is a member function that the base class forces derived classes to
provide. Normally these member functions have no implementation. Pure virtual functions are equated to
zero. class Shape
{
public: virtual void draw() = 0;
};
7
Composition
Composition is again specialize form of Aggregation and we can call this as a death relationship. It is a
strong type of Aggregation. Child object dose not have their lifecycle and if parent object deletes all child
object will also be deleted. Lets take again an example of relationship between House and rooms. House
can contain multiple rooms there is no independent life of room and any room can not belongs to two
different house if we delete the house room will automatically delete. Lets take another example
relationship between Questions and options. Single questions can have multiple options and option can
not belong to multiple questions. If we delete questions options will automatically delete.
How to Implement (Code Example)
Class Circle
{
Point point;
};
Class Point
{
int x;
int y; };
Here Circle is composed of Point.... you can't make a circle without point (Strong dependency)
Distinguish between the terms fatal error and nonfatal error. Why might you prefer to experience
a fatal error rather than a nonfatal error?
A fatal error causes a program to terminate prematurely. A nonfatal error occurs when the logic of the
program is incorrect, and the program does not work properly. A fatal error is preferred for debugging
purposes. A fatal error immediately lets you know there is a problem with the program, whereas a
nonfatal error can be subtle and possibly go undetected.
What are virtual functions? Describe a circumstance in which virtual functions would be
appropriate
Virtual functions are functions with the same function prototype that are defined throughout a class
hierarchy. At least the base class occurrence of the function is preceded by the keyword virtual. Virtual
functions are used to enable generic processing of an entire class hierarchy of objects through a base
class pointer. For example, in a shape hierarchy, all shapes can be drawn. If all shapes are derived from
a base class Shape which contains a virtual draw function, then generic processing of the hierarchy can
be performed by calling every shapes draw generically through a base class Shape pointer.
Given that constructors cannot be virtual, describe a scheme for how you might achieve a similar
effect
Create a virtual function called initialize that the constructor invokes.
8
How is it that polymorphism enables you to program in the general rather than in the specific.
Discuss the key advantages of programming in the general.
Polymorphism enables the programmer to concentrate on the processing of common operations that are
applied to all data types in the system without going into the individual details of each data type. The
general processing capabilities are separated from the internal details of each type.
Discuss the problems of programming with switch logic. Explain why polymorphism is an
effective alternative to using switch logic.
The main problem with programming using the switch structure is extensibility and maintainability of the
program. A program containing many switch structures is difficult to modify. Many, but not necessarily all,
switch structures will need to add or remove cases for a specified type. Note: switch logic includes if/else
structures which are more flexible than the switch structure.
Distinguish between static binding and dynamic binding. Explain the use of virtual functions and
the vtable in dynamic binding.
Static binding is performed at compile-time when a function is called via a specific object or via a pointer
to an object. Dynamic binding is performed at run-time when a virtual function is called via a base class
pointer to a derived class object (the object can be of any derived class). The virtual functions table
(vtable) is used at run-time to enable the proper function to be called for the object to which the base
class pointer "points". Each class containing virtual functions has its own vtable that specifies where the
virtual functions for that class are located. Every object of a class with virtual functions contains a hidden
pointer to the classs vtable. When a virtual function is called via a base class pointer, the hidden pointer
is dereferenced to locate the vtable, then the vtable is searched for the proper function call.