Abstraction
Abstraction
1. Interface.
2. Abstract class.
Interface
Along with abstract methods, an interface may also contain constants, static
methods, and nested types. Method bodies exist only for static methods.
Writing an interface is similar to writing a class. But a class describes the attributes
and behaviours of an object. And an interface contains behaviours that a class
implements.
Unless the class that implements the interface is abstract, all the methods of the
interface need to be defined in the class.
An interface is similar to a class in the following ways −
An interface is written in a file with a .java extension, with the name of the
interface matching the name of the file.
An interface cannot contain instance fields. The only fields that can appear in
an interface must be declared both static and final.
Declaring Interfaces
The interface keyword is used to declare an interface. Here is a simple example to
declare an interface –
Syntax:
Interface InterfaceName
{
Implementing Interfaces
When a class implements an interface, you can think of the class as signing a
contract, agreeing to perform the specific behaviors of the interface. If a class does
not perform all the behaviors of the interface, the class must declare itself as
abstract.
There are mainly three reasons to use interface. They are given below.
o It is used to achieve fully abstraction.
o By interface, we can support the functionality of multiple inheritance.
o It can be used to achieve loose coupling.
Interface Example:
interface MyInterface
System.out.println("implementation of method1");
{
System.out.println("implementation of method2");
obj. method1();
Abstract Class
A class which contains the abstract keyword in its declaration is known as abstract
class.
Abstract classes may or may not contain abstract methods, i.e., methods
without body ( public void get(); )
But, if a class has at least one abstract method, then the class must be
declared abstract.
To use an abstract class, you have to inherit it from another class, provide
implementations to the abstract methods in it.
If you inherit an abstract class, you have to provide implementations to all the
abstract methods in it.
To create an abstract class, just use the abstract keyword before the class keyword,
in the class declaration.
Syntax:
//abstract methods
Abstract Methods
If you want a class to contain a particular method but you want the actual
implementation of that method to be determined by child classes, you can declare
the method in the parent class as an abstract. A method that is declared as abstract
and does not have implementation is known as abstract method.
You have to place the abstract keyword before the method name in the
method declaration.
Syntax:
Any class inheriting the current class must either override the abstract
method or declare itself as abstract.
this.name=nm;
this.gender=gen;
//abstract method
return "Name="+this.name+"::Gender="+this.gender;
this.name = newName;
super(nm, gen);
this.empId=id;
@Override
if(empId == 0){
System.out.println("Not working");
}else{
System.out.println("Working as employee!!");
student.work();
employee.work();
employee.changeName("Pankaj Kumar");
System.out.println(employee.toString());
}
Understanding relationship between classes and interfaces
As shown in the figure given below, a class extends another class, an interface
extends another interface but a class implements an interface.