ABSTRACTION Flashcards
Abstract class in Java
A class which is declared with the abstract keyword is known as an abstract class inJava. It can have abstract and non-abstract methods (method with the body).
It’s needs to be extended and its method implemented. It cannot be instantiated.
Points to Remember
An abstract class must be declared with an abstract keyword.
It can have abstract and non-abstract methods.
It cannot be instantiated.
It can haveconstructorsand static methods also.
It can have final methods which will force the subclass not to change the body of the method.
WHAT Is Abstraction ?
Abstractionis 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 theobjectdoes instead of how it does it.
Ways to achieve Abstraction
There are two ways to achieve abstraction in java
Abstract class (0 to 100%)
Interface (100%)
Abstract Method in Java
Abstract Method in Java
A method which is declared as abstract and does not have implementation is known as an abstract method.
Example of abstract method
abstractvoidprintStatus();//nomethodbodyandabstract
Example of Abstract class that has an 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.
abstractclassBike{
abstractvoidrun();
}
classHonda4extendsBike{
voidrun(){System.out.println(“runningsafely”);}
publicstaticvoidmain(Stringargs[]){
Bikeobj=newHonda4();
obj.run();
}
}
Abstract class having constructor, data member and methods
An abstract class can have a data member, abstract method, method body (non-abstract method), constructor, and even main() method.
File: TestAbstraction2.java
//Exampleofanabstractclassthathasabstractandnon-abstractmethods
abstractclassBike{
Bike(){System.out.println(“bikeiscreated”);}
abstractvoidrun();
voidchangeGear(){System.out.println(“gearchanged”);}
}
//CreatingaChildclasswhichinheritsAbstractclass
classHondaextendsBike{
voidrun(){System.out.println(“runningsafely..”);}
}
//CreatingaTestclasswhichcallsabstractandnon-abstractmethods
classTestAbstraction2{
publicstaticvoidmain(Stringargs[]){
Bikeobj=newHonda();
obj.run();
obj.changeGear();
}
}
Test it Now
bike is created running safely.. gear changed
What is an INTERFACE?
Aninterface in Javais a blueprint of a class. It has static constants and abstract methods.
Interface in Java
Aninterface in Javais a blueprint of a class. It has static constants and abstract methods.
The interface in Java isa mechanism to achieveabstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multipleinheritance in Java.
In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body.
It cannot be instantiated just like the abstract class.
Since Java 8, we can havedefault and static methodsin an interface.
Since Java 9, we can haveprivate methodsin an interface.
Java Interface alsorepresents the IS-A relationship.
Why use Java interface?
There are mainly three reasons to use interface. They are given below.
It is used to achieve abstraction.
By interface, we can support the functionality of multiple inheritance.
It can be used to achieve loose coupling
How to declare an interface?
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.
Syntax:
interface<interface_name>{</interface_name>
//declareconstantfields
//declaremethodsthatabstract
//bydefault.
}
The relationship between classes and interfaces
As shown in the figure given below, a class extends another class, an interface extends another interface, but aclass implements an interface.
Java Interface Example
this example, the Printable interface has only one method, and its implementation is provided in the A6 class.
interfaceprintable{
voidprint();
}
classA6implementsprintable{
publicvoidprint(){System.out.println(“Hello”);}
publicstaticvoidmain(Stringargs[]){
A6obj=newA6();
obj.print();
}
}
Multiple inheritance in Java by interface
Multiple inheritance in Java by interface
If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance
interfacePrintable{
voidprint();
}
interfaceShowable{
voidshow();
}
classA7implementsPrintable,Showable{
publicvoidprint(){System.out.println(“Hello”);}
publicvoidshow(){System.out.println(“Welcome”);}
publicstaticvoidmain(Stringargs[]){
A7obj=newA7();
obj.print();
obj.show();
}
}
) Multiple inheritance is not supported through class in java, but it is possible by an interface, why?
As we have explained in the inheritance chapter, multiple inheritance is not supported in the case ofclassbecause 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.
Interface inheritance
A class implements an interface, but one interface extends another interface.
interfacePrintable{
voidprint();
}
interfaceShowableextendsPrintable{
voidshow();
}
classTestInterface4implementsShowable{
publicvoidprint(){System.out.println(“Hello”);}
publicvoidshow(){System.out.println(“Welcome”);}
publicstaticvoidmain(Stringargs[]){
TestInterface4obj=newTestInterface4();
obj.print();
obj.show();
}
}