Abstraction is an object modeling principle. Through abstraction we will design/declare object operations those are to be implemented by different sub type programmers of this object.
Abstraction will come in to picture at project design phase.
In general abstraction means hiding an object operation implementation details from the user and provides only necessary details to access the object operation is called abstraction.
- Keyboard operations are abstracted- means when we type a key we do not know how characters are transferred and displayed on notepad file.
So we can say abstraction is about” Hiding unnecessary details from the user” and” Taking only what we need at that time by ignoring unnecessary things” is called abstraction.
Important points of abstract method
- We can develop abstraction in java using abstract method by declaring it either in abstract class or in interface.
- Basically abstraction provides a contract document between object user and object implementer.
- By implementing abstraction principle we will provide only method prototypes to user programmer by hide method implementation details in sub classes.
- By declaring method as final we can restrict subclass not to override super class method.
- By creating method as abstract method we can force subclass to override super class method.
A method that has only prototype and doesn’t have body is called abstract method. Abstract method should contain modifier abstract and should ends with semi colon.
For example: abstract void m1();
Note − Eventually, a descendant class has to implement the abstract method; otherwise, you would have a hierarchy of abstract classes that cannot be instantiated.
Suppose grade class inherits the student class, then it should implement the computeResult () method as shown below −
Note: In a class we are allowed only to define methods with body. Since we are changing its default property- means removing its body- it must use modifier abstract keyword in its prototype
- Abstraction is in simple word hiding the implementation from user or client.
- User or client able to see only functionality.
- Like Computer you can only see the keyboard, mouse, display but you cannot able to see motherboard, Ram, SMPS etc.
- We can achieve 100% abstraction by Interface and you can achieve abstraction by Abstract class.
- Now we see what is Abstract class and Interface, how we can achieve 100% abstraction in java.
Abstract Class & Abstract method:
- Abstract class is class with declared with abstract keyword.
- If we achieve 100%, abstraction by abstract class then all method should be abstract only.
- Abstract method means method without implementation.
- In abstract class may or may not be all method abstract.
- This abstract method must always override in subclass or child class otherwise, subclass should be abstract.
- Abstract method must follow abstract key word.
For batter understanding we should go through to the example but in bellow example we will not get 100% abstraction because all method are not abstract.
Yeah shape is Circle
Yeah shape is Squre
Sum of Two number :- 3.5
Sum of Two number :- 4.0
Now we want 100% by using abstract class for that example as bellow.
Yeah shape is Circle
Yeah shape is Squre
Volume for Circle :- 3.5
Volume of Squre :- 4.0
For using above example we can achieve 100% abstraction.
Interface in Java:
- Interface is nothing but is a blue print of class.
- In interface all method are abstract.
- In interface we are using Interface <Class_name> instead of using class <Class_name>.
- Interface also represent a Is A relationship.
- We use interface for getting 100% abstraction.
- Interface is same as a class.
- In interface all method are unimplemented or abstract.
- Java does not support multiple inheritances at that time we use Interface.
- Interface also use for achieve loose coupling.
- In interface variable by default static, final.
- If any Interface does not have method then that interface called us Marker interface.
- For batter understanding just follow the block diagram.
- Through interface, we can achieve 100 % abstraction.
- Now question is arise if we already have abstract class then why we go for interface?
- Because in abstract class may not contain final, static value but in interface by default all values are final, static.
- We cannot create object of interface but we can make reference of it that refer to the object of implementing class.
- Class can possible to implement more than one interface.
- From JDK 1.9 interface can contain following also static method, private method and private static method.
For much understanding let, see one example of interface.
Elephant is Vegiterian
Tiger is Non-vegiterian