Abstraction , Abstract Class and Interface in Java

Abstraction in Javais the process which is used to hide certain details and only show the essential features of the object.Abstraction is like what object does rather than how the object does.Suppose an abstract class has a method which takes amount and return a amount after adding tax ,vat etc.So,the user does not know here how the final amount is calculated,the user only know that he/she will get final amount.

Abstract Class :1 : Abstract class is a class which is declared with abstract keyword.2 : It may or may not have abstract methods3 : Abstract classes cannot be instantiated because they are incomplete.JVM treat a class as incomplete, if it has keyword abstract in its declaration.One will get compile time exception ,while trying to create object of abstract class.4 : Abstract class can be sub-classed.5 : We can not make abstract class as final.If a class is made final ,then we can not extend the class and we can not override the methods of the class.But,abstract class is used by extending it in sub class.So final and abstract are opposite of each other and can not be used together.6 : Abstract class can have constructor in Java and this constructor will be called during constructor chaining by sub class. When sub class extends abstract class and create object ,at that time even if you don’t provide any super class constructor, compiler will add default no argument constructor in abstract class as the first statement in any constructor implicitly calls super(), default super class constructor in Java.Constructor in abstract class can be used to initialize common variables.7 : Abstract class can have static methods.But this method may not be useful as static method can not be overridden.So sub class can not use it.8 : Abstract class can have both concrete and abstract methods.We should use abstract class when we know implementation of some methods and not sure of implementation of other methods in the class.

Example : Consider an abstract class ShapeSize with abstract method perimeter() and concrete method area().The sub class Circle extends the super class ShapeSize and overrides perimeter() method.In the main class we created object of subclass and access both methods.

Program :

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

packagecom.ai1tutorial.java.abstraction;

abstractclassShapeSize{

abstractvoidperimeter();

voidarea(){

System.out.println("Formula for area = π*r*r ");

}

}

packagecom.ai1tutorial.java.abstraction;

classCircleextendsShapeSize{

@Override

voidperimeter(){

System.out.println("Formula for Perimeter = 2*π*r ");

}

}

packagecom.ai1tutorial.java.abstraction;

publicclassAbstractionMain{

publicstaticvoidmain(String[]args){

Circle circle=newCircle();

circle.perimeter();

circle.area();

}

}

OutPut:

Formula forPerimeter=2*π*r

Formula forarea=π*r*r

Interface :

1 : Interface in java is declared using keyword interface.2 : Interface means 100% abstraction.3 : Interface can not have any concrete method.4 :All methods declared inside Java Interfaces are implicitly public and abstract5 :All variables declared inside interface is implicitly public c final variable or constants. 6 :In Java its possible for an interface or class to extend multiple interfaces.7 : We should use interface when we are not sure of implementation of methods in different classes which are going to implement the interface.

Marker interface : 1 : Marker interface in Java is an interface with no field or methods. 2 : These are used to provide necessary information to the JVM so that JVM may perform some useful operation. 3 : For example: Serializable, Cloneable etc.

About the Author

Dear Friends,
I am A.K.M. .This online journal ai1tutorial.com is a humble effort from my side to all students and professionals who want to learn computer science related technologies.I am not a professional blogger and I write posts in this blog only in my leisure time.I may not be 100% perfect in all my posts,but your review comments can surely help me to make it 100% perfect.