Java · OOP · Programming · reference

Object Oriented Programming

Often in the hurly-burly of projects we forget the basic principles of subjects like Object Oriented Programming.
Here I try to capture some of the basic ideas behind the OOPs paradigm and hope it is useful for all.

Q) What are the principles to design a Class?

A) Design a class using SOLID principles.

SOLID stands for:

1. Single Responsibility Principle. One class does one specialized thing.
2. Open Closed Princicple. Class should Open enough to be Extended but Closed to Modification.
3. Liskov’s Substitution Principle. A subclass should be able to substitute it’s super class without altering the desirable properties of program.
4. Interface Segregation Principle. Create Fine Grained Interface, with specialized client in mind.
5. Dependency Inversion Principle. Design class structure from Higher Level classes to Lower Level Classes and connect them using Abstractions (Interfaces). Provides loose coupling.

Q) What is Encapsulation?

A) A class must hide it’s state and the implementation. Typically this is dont by

1. Making the member variables private and providing public getters and setters.
2. By using interfaces instead of classes directly.

Q) What are the two main types of relationships in OOPs?

A) The two main types are:

1. Association. “has-a” relationship. Car has-a wheel.
2. Generalization. “is-a” relationship. Car is-a vehicle.

Q) What is Association?

1. Association is a “has-a” type of relationship between two or more classes i.e. one class is dependent on another class for some functionality.
2. Association is directional in nature, starting from one class to another.

    //Car "has-a" engine
    public class Car {
        Engine e = new Engine();
    public class Engine {}

Association is closely related to other two OOPs concepts viz. Aggregation and Composition.

Q) What is the difference between Association, Aggregation and Composition?

A) Both Aggregation and Composition are special types of Associations.


1. Aggregation is a kind of association that specifies a whole/part relationship between the aggregate (whole) and component part.
2. Aggregation is a “weak” association.
3. The Whole does not completely own the part. Other objects can access the part independently.
4. The Whole does not control the lifecycle of the part. Part lives independently.
5. Example, Pen has a refill but pen does not completely control the refill and refill can live independent of the pen.


1. Composition is a “Strong” association.
2. Composite object completely owns the component thus is forces encapsulation. Other objects can access it only through the composite object.
3. Composite object controls the lifecycle of the component.
4. If composite object is destroyed then all the component parts should also be destroyed or their references passed on to another object.
5. Example, Body is composed of brain. If body dies brain also dies.

Q) What is abstraction?

A) Abstraction means focussing on the higher level ideas or properties rather than low level implementation details.
It is used to reduce the complexity of the system especially during it’s design phase.
Interface and Abstract classes are two main means of achieving abstraction.

Q) What is Generalization?

A) It is a type of OOPs relationship where common properties and functionality is placed in a single entity and then specialized entities are created from it. It is used to create an “is-a” realtionship between classes.In OOPs term this is implemented using inheritance.

For example, Car is-a Type of Vehicle.

Q) What is Specialization?

A) The deriving of specialized classes from generic super class or interface. Ex, Human is a specialization of Animal.

Q) What is an Abstract class?

A) A class which declared abstract and which may contain one or more abstract methods.
In Java a class without any abstract methods can also be declared abstract.
Abstract classes cannot be instantiated.

public abstract class Human {
    public abstract walk();
    public talk() {

Q) What is interface?

A) Interface defines a contract between two objects.
It provides the methods signatures that will be used to communicate between two objects.
It never provides any implementation for any of the methods.
Every interface is implicitly abstract.
All interface members are implicitly public.
All member variables in an interface are implicitly static, final and public. These are ususally declared all uppercase with underscores in between the word.

public interface MyInterface {
    int TOTAL_NUM = 0; // Implicit static, final and public. Declared in uppercase and underscores
    void talk(); // Implicit public
    public void walk();

Q) What is Marker Interface?

A) Marker interfaces are empty interfaces without any method signatures.
These are used to mark a class with special property.
For example, interface if implemented by a class makes it serializable.

Example: Serializable, Clonable, SingleThreadModel

Q) What if a class implements two interfaces with the same method signatures?

A) This will not cause compile time error and will work fine at run time also, as long as the class implements all the methods.

Q) What is implicit and explicit interface implentation?

A) Implicit interface means that your class simply implements the methods of an interface.
Explicit interface implementation means that you actually write code to implement a specific interface.

Java ONLY SUPPORTS Implicit interface implementation. C# supports both.

/* Java supports only implicit interface implementation */
public interface Abc {
    public void foo();

public class Xyz implements Abc {
    public void foo() {
        //do something.

Q) What is inheritance?

A) Inheritance is the implementation mechanism for Generalization-Specialization relationships in OOPs.
Inheritance is of two types

1. Single Inheritance. Java supports only single inheritance.
2. Multiple Inheritance. C++ supports multiple inheritance.

public interface Mammal {
    public void eat();

public class Human implements Mammal {
    public void eat() {
        //some code

Q) What is Polymorphism?

A) Polymorphism is a Greek word meaning Poly(Many) and Morph(Forms).
In OOPs polymorphism means the same object reacts to the same messages in different ways.

Q) What are the three ways to achieve Polymorphism in OOPs?

A) Three ways to achieve polymorphism are:

1. Method Overloading
2. Operator Overloading
3. Method Overriding

Q) What is Method Overloading?

A) Ability to define multiple methods with the same name and different signatures.
In Java, overloading must follow the rules given below:

1. The number of arguments and the order of types must be different for each method.
2. In case the parameters are of similar type, e.g. int and long then the minimum wide type is called.
3. Return types cannot be used for overloading. Compiler error is thrown if two methods are different only by return type.
4. Exception types thrown are also not considered for overloading. Compiler error.

Q) What is Operator overloading?

A) Also called ad-hoc polymorphism. Ability to re-implement the mathematical operators such that they return different results based on the parameters passed.

Java DOES NOT support operator overloading.

Q) What is method overriding?

A) Method overriding occurs when a subclass implements a method that has the exact signature as that in the super class.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s