Signed in as:
filler@godaddy.com
Signed in as:
filler@godaddy.com
A class which is declared with the abstract keyword is known as an abstract class in Java. It can have abstract and non-abstract methods (method with the body).
Before learning the Java abstract class, let's understand the abstraction in Java first.
Abstraction is a process of hiding the implementation details and showing only functionality to the user.
Another way, it shows only essential things to the user and hides the internal details, for example, sending SMS where you type the text and send the message. You don't know the internal processing about the message delivery.
Abstraction lets you focus on what the object does instead of how it does it.
There are two ways to achieve abstraction in java
A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.
Example of abstract class
A method which is declared as abstract and does not have implementation is known as an abstract method.
Example of abstract method
In this example, Bike is an abstract class that contains only one abstract method run. Its implementation is provided by the Honda class.
Test it Now
running safely
In this example, Shape is the abstract class, and its implementation is provided by the Rectangle and Circle classes.
Mostly, we don't know about the implementation class (which is hidden to the end user), and an object of the implementation class is provided by the factory method.
A factory method is a method that returns the instance of the class. We will learn about the factory method later.
In this example, if you create the instance of Rectangle class, draw() method of Rectangle class will be invoked.
File: TestAbstraction1.java
Test it Now
drawing circle
File: TestBank.java
ADAD
Test it Now
Rate of Interest is: 7 %
Rate of Interest is: 8 %
An abstract class can have a data member, abstract method, method body (non-abstract method), constructor, and even main() method.
File: TestAbstraction2.java
Test it Now
bike is created
running safely..
gear changed
Test it Now
compile time error
The abstract class can also be used to provide some implementation of the interface. In such case, the end user may not be forced to override all the methods of the interface.
Test it Now
Output:I am a
I am b
I am c
I am d
==============================================================================
An interface in Java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java.
In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body.
Java Interface also represents the IS-A relationship.
It cannot be instantiated just like the abstract class.
Since Java 8, we can have default and static methods in an interface.
Since Java 9, we can have private methods in an interface.
There are mainly three reasons to use interface. They are given below.
An interface is declared by using the interface keyword. It provides total abstraction; means all the methods in an interface are declared with the empty body, and all the fields are public, static and final by default. A class that implements an interface must implement all the methods declared in the interface.
Since Java 8, interface can have default and static methods which is discussed later.
ADAD
In other words, Interface fields are public, static and final by default, and the methods are public and abstract.
As shown in the figure given below, a class extends another class, an interface extends another interface, but a class implements an interface.
In this example, the Printable interface has only one method, and its implementation is provided in the A6 class.
Test it Now
Output:
Hello
In this example, the Drawable interface has only one method. Its implementation is provided by Rectangle and Circle classes. In a real scenario, an interface is defined by someone else, but its implementation is provided by different implementation providers. Moreover, it is used by someone else. The implementation part is hidden by the user who uses the interface.
File: TestInterface1.java
Test it Now
Output:
ADAD
drawing circle
Let's see another example of java interface which provides the implementation of Bank interface.
File: TestInterface2.java
Test it Now
Output:
ROI: 9.15
If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance.
Test it Now
Output:Hello
Welcome
As we have explained in the inheritance chapter, multiple inheritance is not supported in the case of class because of ambiguity. However, it is supported in case of an interface because there is no ambiguity. It is because its implementation is provided by the implementation class. For example:
ADAD
Test it Now
Output:
Hello
As you can see in the above example, Printable and Showable interface have same methods but its implementation is provided by class TestTnterface1, so there is no ambiguity.
A class implements an interface, but one interface extends another interface.
Test it Now
Output:
Hello
Welcome
Since Java 8, we can have method body in interface. But we need to make it default method. Let's see an example:
File: TestInterfaceDefault.java
Test it Now
Output:
drawing rectangle
default method
Since Java 8, we can have static method in interface. Let's see an example:
File: TestInterfaceStatic.java
Test it Now
Output:
drawing rectangle
27
An interface which has no member is known as a marker or tagged interface, for example, Serializable, Cloneable, Remote, etc. They are used to provide some essential information to the JVM so that JVM may perform some useful operation.
Note: An interface can have another interface which is known as a nested interface. We will learn it in detail in the nested classes chapter. For example:
==============================================================================
Abstract class and interface both are used to achieve abstraction where we can declare the abstract methods. Abstract class and interface both can't be instantiated.
But there are many differences between abstract class and interface that are given below.
Abstract class
1) Abstract class can have abstract and non-abstract methods.
2) Abstract class doesn't support multiple inheritance.
3) Abstract class can have final, non-final, static and non-static variables.
.4) Abstract class can provide the implementation of interface.
5) The abstract keyword is used to declare abstract class.
6) An abstract class can extend another Java class and implement multiple Java interfaces.
7) An abstract class can be extended using keyword "extends"
8) A Java abstract class can have class members like private, protected, etc.
Interface
1)Interface can have only abstract methods. Since Java 8, it can have default and static methods also.
2)Interface supports multiple inheritance.
3)Interface has only static and final variables
4)Interface can't provide the implementation of abstract class.
5)The interface keyword is used to declare interface.
6)An interface can extend another Java interface only.
7).An interface can be implemented using keyword "implements".
8)Members of a Java interface are public by default.
.9)Example:
public abstract class Shape{
public abstract void draw();
}Example:
public interface Drawable{
void draw();
}
Simply, abstract class achieves partial abstraction (0 to 100%) whereas interface achieves fully abstraction (100%).
Let's see a simple example where we are using interface and abstract class both.
Test it Now
Output:
I am a
I am b
I am c
I am d
We use cookies to analyze website traffic and optimize your website experience. By accepting our use of cookies, your data will be aggregated with all other user data.