Object Oriented Programming (OOP)

Object-oriented programming is a way of programming in which we think each object as a programming object. There are 3 major benefits of it:

1. It makes code more reusable.
2. It makes code more maintainable.
3. It makes it easy to program. As it is near to real world so we map real world concept to software concept.


Class is a template, specification, or type of an object.


An object is anything which has characteristics (properties) and behaviors.


It is a special type of method. It is automatically called when instance (object of a class) is created. It is mainly used for initialization. Note that constructors never get inherited. Besides, by default there is a default constructor but if we overload constructor then to have zero-parameter constructor, we have to explicitly make zero-parameter constructor.


When two classes belong to the same type (category) then inheritance comes into being. It is called “is a” relationship.
For example, Cashier is an employee. In addition to it, Sales-person is an employee.
Note that if there is some extra attribute etc. in Cashier or Salesperson in their own then we would do sub-classing (inheritance), otherwise Employee class would be enough and we should not create Cashier and Salesperson class.

Everything which is declared as public or protected in the parent class, will be inherited to the child class except constructors.


If lifetime of a part is dependent on the whole then it is called composition. For example, Sale Line Item is a part while Sale is a whole. Similarly, Finger is a part and Hand is a whole. Composition is also called “has a” relationship.


If lifetime of an object is not totally dependent on other object then it is called aggregation. It is also called shared composition.


It is about getting different behavior. According to it, reference variable can behave differently depending on the object it is pointing to. There are 2 rules for polymorphism:

1. Method must be overridden.
2. Reference variable of super class (parent class) should be used.


In an interface, all methods are public and abstract only. It has following 3 usage:

1. It is used to support for polymorphism.
2. It is a better alternative to multiple- inheritance.
3. It is used to define a contract of an object.

Method Overloading

Same method name but with different signature is called method overloading.

Method Overriding

Method overriding is about changing behavior of a super-class method. There are 4 rules for method overriding:

1. Method must be inherited.
2. Signature (name, parameters etc.) should be same.
3. Return type should be same.
4. Access modifiers should be same.

In this case, method of parent class is called Overridden method while method of child class is called Overriding method.

5 out of 5 based on 13 user ratings