Is Data Abstraction and Encapsulation a fancy term of Information Hiding?

I have seen many developer/Architect use the term interchangeably and has the reason for it but yes there are differences-- a huge difference in terms of hiding information. I try to explain it in a Simple way.
Let’s start by the definition.
Encapsulation: binds data and behaviors together in a Single unit and behaviors acts on the data.
Abstraction: Hiding the implementation details and expose the functionality to the world.

According to both definition, both try to hide data from rest of the world and expose some behaviors/method so other System/API can use it.

At this point Both are same so If someone uses those term Interchangeably they are correct.
But hold why then Two fancy concepts are side by side in OOP why they are not merged into a one and called it “Information hiding

To understand the same take a look Suppose you have to implement a car.
So when you rotate the steering lots of thing happening inside and eventually car is moving to the direction you rotate the steering.
Now let explain the Action in details
Input: Steering rotation direction.
Output: car moves in the Direction steering rotated.
but what is happening inside is a black box to the user—That is called Abstraction
So technically it says What to abstract from User?
Abstraction is a functionality which helps the developer to identify what is the functionality that should be abstracted and exposed as a function/method which takes User input and returns desired result what User wants.
In a car Steering functionality, Braking functionality, Auto parking --these are the functionalities has to be abstracted from User— User less interested How it works but what they interested is What should I do(Input) and What will be the Outcome. So according to me Abstraction is

Abstraction:  By Abstraction, developers identify the functions what should be published in API and what input it takes and What Output it returns.

So, another point of view is, Abstraction helps us to the generalization of a functionality-- So When you design a function’s input or output you should be very careful about the data type you used-- It should be supported all possible combination on which function can be applied.

Now come to Encapsulation It tells about How to achieve the functionality-- which has been identified by Abstraction.

So it tells us about the packaging the data and behaviors.
Take the same example use steering to move the car.
Encapsulation: identifies the different parts associate to move the car using user instruction like steering, Wheel, Engine.Petrol . Also, it identifies the algorithm/behaviors which will be applied to these data(wheel, steering, engine, petrol) to move the car, and help to binds or packaging as one single unit. In my perspective Encapsulation definition is.

Encapsulation:Encapsulation Helps to understand what are the data and functions, that should be bundled as a Single Unit so User can act on them without knowing internal details and get the job done.
Information Hiding
Explanation of the figure: When you design an API/Class always there is two perspective one is Developers View and one is API User view. From Developers View Abstraction is to identify the features to be provided and Encapsulation is the process to communicate with internals things and provide the functionality. So it makes sense to have two distinct terminology Abstraction and Encapsulation.

But for User of the API/Class, It is like what functionality is exposed and what is the input and what will be the output so functionality an API provides nothing but Opaque things to them they provide input and got Output --API or Class is a barrier or Facade for them So for them it is just an Information hiding so Abstraction and Encapsulation has no meaning for them. It can be used alternatively to mention information hiding.

Conclusion :  Abstraction and Encapsulation both are used for hiding information context but their purpose is different.  Abstraction helps to understand the functionality User interested for and providing the same to the user as a black box. Encapsulation is about the gathers the required data and algorithm to solve the purpose for the user and tied them in a single Unit so the user of the API  doesn't have to collects the data and apply the algorithm by itself to get the job done.

Overriding in Java in context of design.

Overriding in Java in context of design.


 While you are going for an Interview for a Lead or Developer role. The interviewer will definitely ask you some questions on Overriding and try to puzzle you.

In this tutorial, I try to cover all topics of Overriding so you feel comfortable and easily answer  whatever questions they ask

Definition of Overriding:
In Java, Overriding is a technique by which parent class delegates responsibility to Child. A child can define child specific behavior which will be executed at run time.

From the above definition, it is clear that to do Overriding Parent class and child class is must need, without Child class, we can't achieve overriding, unlike overloading.

Please remember , The interviewer may ask you-- can it be possible to do overriding without child class?

As I said child specific behavior executed at run-time what I mean by this is the  JVM will decide which  Subclass/Object's method it has to call,  so we call it late binding or Dynamic method dispatch.Because runtime JVM decides what to do not at compile time.

Question: What is late binding/ Dynamic method dispatch in Java

If you don't get my point don't worry follow example will clear the concept.

Before dig down to Overriding details let clear one small thing.

The big power of OOP programming is it supports Polymorphic assignment where a parent class reference can point a Subclass object. It will be heavily used in Design pattern or I can say the Whole Design pattern is based on this small concept.


Q: What do you mean by polymorphic assignment and Why it is important?

Let's understand why this is important.

say  Foo is Parent class and Bar is child class so we can do that,

Foo foo = new Bar();//Polymorphic assignment.

By parent reference, we can point child Object. It is logical as child inherited all properties of the parent so whatever in parent must have in the child unless properties are private in a parent.

What is the benefits of polymorphic assignment?
 let see the small code

Class Power

private Foo f;

public setFoo(Foo f)
this.f = f;


public void show()

Class Foo has a print() method which is overridden in Bar and Zoo two subclasses of Foo

overridden  in Bar
public void print()

overridden in Zoo
public void print()

overriding java

so by the setFoo method in Power class, I can easily set Bar or Zoo instance at run time based on some business logic .so when it calls show method based on the subclass it prints Bar or Z

So I can easily change the behavior of the Power class on the fly based on strategy. It is the crux of Strategy design pattern.

But remember, As SetFoo method takes parent reference what ever method are in parent class  you can call those, all Association condition applied here. So if any new method added in subclass that is not visible by parent reference.

Qustion : expect a coding snippet here where interviewr add a method in child and try to call it by polymorphic refrence.

To make it visible you need to do down-casting.

Tip: Always use @Override annotation if you are using Java 5 or more to make sure if maintains all rules of overriding.

To do a perfect overriding we have to follow some rules.

The method which will be overridden must have same data signature also return type will be same

but onwards java 1.5 return type can be subclasses of parent type we say it Co-variant return.
 Q: What is covarient return?

Please follow the following contracts and burn it into the head as in Interview or in Exam you can found various types of combination and you have to find the perfect overridden method.
1. The argument list must exactly match that of the overridden method. If they
don't match, you can end up with an overloaded method .

2. The return type must be the same as, or a subtype of, the return type declared
in the original overridden method in the superclass. we say it covariant return

3. The access level can't be more restrictive than the overridden methods.
The access level CAN be less restrictive than that of the overridden method.
Instance methods can be overridden only if they are inherited by the subclass.
A subclass within the same package as the instance's superclass can override
any superclass method that is not marked private or final. A subclass in a
different package can override only those non-final methods marked public
or protected (since protected methods are inherited by the subclass).

4. The overriding method CAN throw any unchecked (runtime) exception,
regardless of whether the overridden method declares the exception.

5. The overriding method must NOT throw checked exceptions that are new
or broader than those declared by the overridden method. For example, a
the method that declares a FileNotFoundException cannot be overridden by a
the method that declares an SQLException, Exception, or any other non-runtime
exception unless it's a subclass of FileNotFoundException.

6. The overriding method can throw narrower or fewer exceptions.

Understand protected modifier in java

Understanding most critical access modifier  protected in java?

We all know Java has three access modifiers a, public b. protected c. private and four access levels  a.public b. protected c. private d. default among them protected is the most critical modifier to understand.

Definition of protected
protected is same as default modifier, which can be accessed by other classes in the same package but the only difference is it can also be accessed by sub classes from outside packages through inheritance.

Now there are two catch points

1. What do you mean by accessed?
2. What do you mean by access through inheritance?

1. What do you mean by accessed?

In Java, we can access properties of other classes by two ways

a. Association (HAS A)
b. Inheritance (IS-A)

Scenario a.

public class Bar
 protected String greet = "Hello";

public class Foo extends Bar
Bar bar = new Bar(); // Bar HAS A relationship with Foo(Association)

bar.greet; //not compiled as no visibility through association


Scenario b

Class Bar
 protected String greet = "Hello";

Class Foo extends Bar

System.out.println(greet); //accessd through Inheritence so it prints Hello


Association means a class has a reference to another class  as you see in Scenario a.

Inheritance means  a class inheriting parent class property.
The Answer is  no as by rule protected is visible only through inheritance so although class Foo extends Bar but we are trying to access greet variable through association which is not acceptable in Java

On another hand, in Scenario 2 will compile fine and show Hello as Foo inherited Bar's greet property though Inheritance by definitions which is acceptable.

Now, adding bit complexity in it
the class zoo is in the package now If I write following code snippet in Zoo

Foo foo = new Foo();

will it be compile?

Think about it.......

The Answer is no as I said earlier by Association (different package ) you can't ever access greet. but in Same package you do.

See the relationship picture to remember the rule.

Protected access modifiers