Abstract and Interface in java

Posted By : Hany Yadav | 30-Nov-2020

Java

Loading...

Abstract Class

1- An abstract class is a class in java which is declared with the abstract keyword is known as abstract class.

2- It contains abstract and non-abstract methods (method with body).

3- The abstract class cannot be instantiated; it is only extended and its method implemented.

4- The abstract class may contain constructor and static methods.

5- In Java, an abstract class may contain final methods also and an abstract method does not have implementation.

Abstraction

Abstraction is the process of hiding the actual implementation details from the user and showing only necessary functionality to the user. It hides the internal details and shows the essential details that the user requires.

Example of abstract class:-

abstract class A{}

Abstract Method:-

Abstract void print(); // Method without body

Example:-

Interface A{

Void a();

Void b();

Void c();

}

abstract class B implements A

{

public void a()

{

System.out.println("I am fine");

}}

Class Z extends B{

public void b()

{

System.out.println("I am working");

}

public void c(){

System.out.println("I am playing");

}

Class Test{

public static void main(String args[]){

A a = new Test(); // creating object of class

a.a();

b.b();

c.c();

}

Output:-

I am fine

I am working

I am playing

Interface

1-In java interface is just like a class which contains methods without body and it is also used to hide the actual implementation to the end user and it is used to group similar methods without body.

2- In java an interface is declared with the keyword "Interface" and it is one of the reference types in java.

3- By using the interface we can achieve "Total Abstraction" and it cannot contain any constructor.

4- An interface in java can be accessed only when it is implemented by another class by using the "Implements" keyword instead of using "extends" keyword.

Total Abstraction

In java "Total Abstraction" means all the methods in an interface are declared with an empty body.

5- In Interface by default all the fields are public, static and final.

6- An interface can extend another interface in the same manner just like a class can extend another class.

Example:-

Interface printtable{

void print();

}

Class Test implements printtable{

public void print(){

System.out.print("Interface");

}

public static void main(String args[]){

Test obj = new Test();

obj.print();

}}

Output:-

Interface

We, at Oodles ERP, provide end-to-end ERP development services to overcome complex enterprise challenges and streamline inbound/outbound operations. Our custom ERP software development services enhance your business productivity and improve operational efficiency. For more information, contact us at [email protected].