Java abstract class and interface with easy explanation

In this tutorial,we will learn about the java abstract class and interface with easy explanation step by step & also what is the difference between an abstract class and interface. 

Before learning the abstract class in java,we try to understand abstraction in java first.

Abstraction in java

It is the process of hiding the internal details and showing the functionality only.

Example:

When one person talks to another person through mobile then we don’t know how mobile internally works but we only use the phone ie use the functionality of mobile. 

Java Abstract class

1.In a java,abstract class is created using the “abstract” keyword.

2.Abstact class method may be abstract or non-abstract.

3.By the abstract class we can achieve 0 to 100 % abstraction. 

Ie

It is a class which  is created using the “abstract” keyword that contains the abstract(without body) or non-abstract(with body)  method.

Key points to remember about abstract class

1.abstract method can be implemented by extending the abstract class into the other class.

2.Abstarct class must have “abstract keyword” before the class.

3.It contains abstract and non-abstract method.

4.It also contains constructor and static method.

Example of abstract class

abstract class Test
{
}
Example of abstract method

abstract class Test
{
abstract void run( );//This is an abstract method without body
}


Example of an abstract class with an abstract method non-abstract method

abstract class Test
{
abstract void run( )
Public void show( )
{
System.out.println(“Hello placement”);
}
}

public class  ExtendTest extends Test
{
public run( )
{
System.out.println(“Hii preparation”);
}
Public static void main(String[ ] args)
{
Test obj=new ExtendTest( );
obj.show( );
obj.run( );
}
}

Output:

Hello placement

Hii preparation

Java interface

It is the blueprint of class that contains fully abstract method (without body) and static,final and public  variables.

It is created using the “interface” keyword.

Why use interface

1.To achieve fully abstraction

2.To achieve multiple inheritance

3.To achieve loose coupling

4.It represents IS-A relationship

Note:

Abstract class contains non-final variables.

Interface contains final,static and public variables. 

In Java 8,it contains default and static method

In Java 9,it contains private method.

Syntax

Interface Test

{

//static/public/final variable

//fully abstract method
}

Interface is created using the “interface” keyword.

How  to implement the abstract method of interface .We have to implement the abstract method into the class with the help of “implement” keyword

Note:  class1 extends class2

           class1 implements interface1

           class1 extends class2  implements interface1,interface2,interface3

Example of interface


interface Test
{

final int a=20;//This is the final  variable
void run ( ); //This is an abstract method
}

public class NewTest implements Test
{
public run( )
{
System.out.println(“Hello placement preparation”);
}
Public static void main(String[ ] args)
{
NewTest obj=new NewTest( );
obj.run( )
System.out.println(a);
}
}

Output

Hello placement preparation

20

What are the differences between the abstract class and interface ?


Abstract class

Interface


1.To create abstract class we use “abstract” keyword.


1.To create interface,we use “interface” keyword.


2.It is used to achieve abstraction 0 to 100%


2.It is used to achieve fully abstraction ie 100%


3.In abstract class,variable may be non-final,static,non-static


3.In interface,variable may be static,final and public


4.it contains abstract or non-abstract method


4.It contains only abstract method.


5.It extends the java class and implements the multiple interfaces.


5.It extends only other interface.


6.Example
public abstract class Test
{
abstract void run( );
}

6.Example
Interface Test
{
void run( );
}

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *