Recent

11.Abstraction in Java

Abstraction In Java

 

Abstraction is an OOPs concept which is used to hide implementation details, show functionality and hide complexity. for example: a phone call – we do not know how the internal process takes place.

Abstraction is also used to define standards.

Ways to achieve abstraction in Java

There are two ways to achieve abstraction in Java:

·         Abstract class

·         Interface

 

Abstraction via abstract class

Question: What is an abstract class?

Answer: It is a normal class which is defined by using abstract keyword is known as Abstract class. An abstract class cannot be instantiated, it means we cannot make its object.

Question: How is this abstract class different from a normal class?

Answer: Differences between abstract class and a Normal class:

1.    Abstract class cannot be instantiated but we can create object of normal class.

2.    To use abstract methods of an Abstract class, some another class should inherit this class. And child has to override all the abstract method of abstract class otherwise it has to make itself abstract.

3.    Only Abstract class can have abstract methods(methods without a body).

Question: can we make any abstract class as private?

Answer: No.

Question: Can we make any static method as abstract?

Answer: No.

Question: can you keep a constructor in a abstract class?

Answer: yes, in Java we have constructor in every class. If we don’t do this then inheritance cannot happen.

An Example of Abstraction via Abstract Class:

In this program, we did not provide the body of abstract method in child class. that is why this program will give compilation error. We need to override the abstract method in child class.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

abstractclassDemo

{

abstractvoidshow();

staticvoidview()

{

System.out.println("Hello View");

}

}

 

classDemo2extendsDemo

{

publicstaticvoidmain(String[]args)

{

Demo2d=newDemo2();

d.view();

}

}

 Output:

Now after overriding the method in child class:

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

abstractclassDemo

{

abstractvoidshow();

 

staticvoidview()

{

System.out.println("Hello View");

}

}

 

classDemo2extendsDemo

{

 

voidshow()

{

System.out.println("Hello from Demo 2");

}

 

publicstaticvoidmain(String[]args)

{

Demo2d=newDemo2();

d.view();

d.show();

}

}

 Output:

 

Constructors in Abstract class:

Abstract class can also have constructors in it. They can have data members, constructors, methods with body and even main() method.

See the example of Abstraction with constructors:

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

abstractclassDemo

{  

 

Demo()

{

System.out.println("I am watching you");

}  

 

abstractvoidview();

  

voidshow()

{

System.out.println("show");

}  

}  

  

classDemo2extendsDemo

{  

 

voidview()

{

System.out.println("I am still watching");

}  

 

}  

 

classDemo3

{  

publicstaticvoidmain(Stringargs[])

{  

Demo2d=newDemo2();

d.show();  

d.view();  

}  

}  

OUTPUT:

In this program the “Demo2″ class inherits the class “Demo”. So before execution of its constructor, its parent’s constructor will be executed.

Abstraction via Interface

With abstract class, we were not able to achieve 100% abstraction in Java but with the help of Interfaces we can achieve 100% abstraction in Java.

Question: What is an Interface?

Answer:

1.    Interfaces are the contract between the Java program and the concepts of Java.

2.    Interface is just like a class but not class. We can say that they are the blue prints of a class.

3.    They are also used to define standard like abstract class.

Question: By default all the non static methods of an interface are abstract and public. Why?

Answer: The non static methods of an interface are abstract to achieve fully abstraction and they are public so that they can be accessed from outside the class.

Question: What is the difference between Abstract class and interface?

Answer:

1.    Abstract class do not support multiple inheritance but with the help of interface, it is possible to implement multiple inheritance in Java (see the example below).

2.    In an abstract class we can have final, static and non static variable but in case of interface, we can have only static and final variable.

3.    “abstract” keyword is used to define an abstract class and “interface” keyword is used to define an interface.

4.    Abstract class can have abstract methods and non abstract methods but in case of Interfaces, they can have only abstract methods.

 

An Example of abstraction via interface:

To define an “interface”, Java has given a keyword “interface”. and as we know that interfaces are contracts between a Java program and the concepts of Java. So, Java also given a keyword to implement this interface and the keyword is “implements”.

In the program below, we created an interface with name “My” and class Child is implementing this interface.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

interfaceMy

{

voidshow();

}

 

classChildimplementsMy

{

 

publicvoidshow()

{

System.out.println("show");

}

 

publicstaticvoidmain(String[]args)

{

Mym=newChild();

m.show();

}

 

}

Output:

Multiple Inheritance in Java with Interfaces

Consider this example to understand this: Read Comments…

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

39

40

41

42

43

44

45

interfaceMy

{

voidshow();   //show method defined in interface

}

 

interfaceNew

{

voidshow();   //same method defined in another interface

}

 

interfaceAnotherextendsNew,My  //multiple inheritance

{                       //one interfaces extending more than one interfaces.

voiddisplay();  //method of this interface

}

 

//Child class implements Another interface

//now child class will have to give the body of all abstract methods

// of My interface, New interface and Another interface

//As we can see that there is show() method common in both the

//interfaces so we have to give its body once.

classChildimplementsAnother  

{

publicvoidshow()

{

System.out.println("show method");

}

 

publicvoiddisplay()

{

System.out.println("display method");

}

 

publicstaticvoidmain(String...s)

{

Mym=newChild();

m.show();

 

Newn=newChild();

n.show();

 

Anothera=newChild();

a.show();

a.display();

}

}

Output:

POINTS TO REMEMBER

1.    A child class has to give the body of all the non static methods of an interface or it has to overrides the class methods.

2.    A child class can implement more than one interface simultaneously.

3.    If a child class having same method from more than one interfaces then it has to give the body only once.

4.    One interface can extends another interface also. and one interface can extends more than one interfaces simultaneously and this situation/concept which proves multiple inheritance in Java.

No comments:

Post a Comment

Bug-dbug Designed by Bugdbug - Developed by Belson Raja Copyright © 2016

Theme images by Bim. Powered by Blogger.
Published By Bugdbug India