Difference Between Abstract class and Interface OOP

In the previous tutorial, you have learned the difference between overloading and overriding in Java. In this tutorial, you will learn about the difference and use of Abstract class and Interface.

Abstract class and Interface OOP

Abstract Class:

A class is called abstract when it is declared as an abstract class using abstract keyword.

It’s not necessary to declare all the methods in the abstract class as abstract methods. But when a method is declared as an abstract method in abstract class then it has no body and the class which extends that abstract class must have to define that abstract method.

It is the same concept as we use virtual and pure virtual functions in C++ to make a class abstract.

Syntax:

1

2

3

abstractclassCountry{

abstractpublicvoidcity();

}

As in the above syntax abstract is a keyword in Java use to declare a class as abstract This class contain one abstract method city (). It is not a necessary to declare all methods abstract in an abstract class, but when some class extends that class, then it should implement all the abstract functions of the abstract parent class.

1

2

3

classProvinceextendsCountry{

voidcity(){…}

}

The abstract class can inherit only one class or one abstract class at a time. It can have both abstract and concrete methods. In the abstract class, the keyword ‘abstract’ is compulsory to declare a method as an abstract. It can have protected and public abstract methods. It can have static, final, non-static or non-final variables with any access specifies.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

packageabstractjava;

abstractclassPerson{

voideat(){

System.out.println("This Person can eat");

}

abstractvoidrun();

}

classTeenagerextentsPerson{

voidrun(){

System.out.println("This perosn can run");

}

}

classmainclass{

publicstaticvoidmain(Stringargs[]){

Teenager obj=newTeenager();

obj.run();

obj.eat();

}

}

Interface:

An Interface is a blue print of a class. It contains only abstract methods. Its members should not be static. It has no constructor. An interface is a reference type in Java, it is similar to class, and it is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.

Syntax:

1

2

3

4

5

interfaceCountry{

voidcity();

}

Properties of Interfaces

Interfaces have the following properties:

An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an interface.

Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.

Methods in an interface are implicitly public.

The class uses the implements keyword to implement an interface. The implements keyword appears in the class declaration. Like above we create an interface name Country, now to implement the interface.

1

2

3

classProvinceimplementsCountry{

voidcity(){…}

}

The interface can extend any number of interfaces at a time. A class can implement more than one interface. Interfaces have only abstract methods. A class can implement any number of interfaces. In an interface keyword, ‘abstract’ is optional to declare a method as an abstract. The interface cannot have access specifiers, have only public abstract methods i.e. by default.

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

//create interface

packageMyPack;

interfacePerson{

publicvoideat();

publicvoidrun();

}

//create class teenager to implement interface

packageMyPack;

classTeenagerimplementsPerson{

@Override

publicvoideat(){

System.out.println("This perosn can eat");

}

@Override

publicvoidrun(){

System.out.println("This perosn can run");

}

}

//create main

packageMyPack;

publicclassMain{

publicstaticvoidmain(StringArgs[])

{

Teenager obj;

obj=newTeenager();

obj.run();

obj.eat();

}

}

Abstract class can also implement an interface

As you know that abstract classes can have abstract methods as well as concrete methods, so when an abstract class implements an interface it must override and implement all the methods of the interface. You can understand this concept through the example below.

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

//Create interface

packageMyPack;

interfacePerson{

publicvoideat();

publicvoidrun();

}

//create abstract class to implement interface

packageMyPack;

abstractclassTeenagerimplementsPerson{

@Override

publicvoideat(){

System.out.println("This perosn can eat");

}

@Override

publicvoidrun(){

System.out.println("This perosn can run");

}

}

//create main and implent un implemented methods

packageMyPack;

publicclassMain{

publicstaticvoidmain(StringArgs[])

{

Teenager obj;

obj=newTeenager(){};

/*a subclas is required by compiler to implement all its methods that is why some methods could not be implemented*/

obj.run();

obj.eat();

}

}

One interface can extend another interface

One interface can also extend another interface, which means that two interfaces can have one or more functions in common. The example below explains the concept of interface extending another interface.