[go: up one dir, main page]

0% found this document useful (0 votes)
21 views12 pages

Abstract Class & Interface

An abstract class in Java is declared with the abstract keyword and can contain both abstract and non-abstract methods, but cannot be instantiated. Abstraction in Java allows hiding implementation details while exposing only functionality, achievable through abstract classes and interfaces. Interfaces serve as blueprints for classes, supporting abstraction and multiple inheritance, with methods being public and abstract by default.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views12 pages

Abstract Class & Interface

An abstract class in Java is declared with the abstract keyword and can contain both abstract and non-abstract methods, but cannot be instantiated. Abstraction in Java allows hiding implementation details while exposing only functionality, achievable through abstract classes and interfaces. Interfaces serve as blueprints for classes, supporting abstraction and multiple inheritance, with methods being public and abstract by default.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Abstract class in Java

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).

Abstraction in Java

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.

Ways to achieve Abstraction

There are two ways to achieve abstraction in java

Abstract class (0 to 100%)

Interface (100%)

Abstract class 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.

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 have constructors and static methods also.

It can have final methods which will force the subclass not to change the body of the
method.

Example of abstract class

abstract class A{}


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

abstract void printStatus();//no method body and abstract

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.

abstract class Bike{

abstract void run();

class Honda4 extends Bike{

void run()

System.out.println("running safely");

public static void main(String args[]){

Bike obj = new Honda4();

obj.run();

Output

running safely

Understanding the real scenario of Abstract class


In this example, Shape is the abstract class, and its implementation is provided by the
Rectangle and Circle classes.

In this example, if you create the instance of Rectangle class, draw() method of Rectangle
class will be invoked.

abstract class Shape{

abstract void draw();

//In real scenario, implementation is provided by others i.e. unknown by end user

class Rectangle extends Shape{

void draw(){System.out.println("drawing rectangle");}

class Circle1 extends Shape{

void draw(){System.out.println("drawing circle");}

//In real scenario, method is called by programmer or user

class TestAbstraction1{

public static void main(String args[]){

Shape s=new Circle1();

s.draw();

Output

drawing circle

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.
//Example of an abstract class that has abstract and non-abstract methods

abstract class Bike{

Bike()

System.out.println("bike is created");

abstract void run();

void changeGear()

System.out.println("gear changed");

//Creating a Child class which inherits Abstract class

class Honda extends Bike{

void run()

System.out.println("running safely..");

//Creating a Test class which calls abstract and non-abstract methods

class TestAbstraction2{

public static void main(String args[]){

Bike obj = new Honda();

obj.run();

obj.changeGear();

}
}

OUTPUT

bike is created

running safely..

gear changed

Rule: If there is an abstract method in a class, that class must be abstract.

class Bike12{

abstract void run();

Output

compile time error

Rule: If you are extending an abstract class that has an abstract method, you must either
provide the implementation of the method or make this class abstract.

Interface in Java

An interface in Java is a blueprint of a class. It has public 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.


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>{

// declare constant fields

// declare methods that abstract

// by default.

Internal addition by the compiler

The Java compiler adds public and abstract keywords before the interface method.
Moreover, it adds public, static and final keywords before data members.

In other words, Interface fields are public, static and final by default, and the methods are
public and abstract.

The relationship between class and interface

Java Interface Example


In this example, the Printable interface has only one method, and its implementation is
provided in the A6 class.

interface printable{

void print();

class A6 implements printable{

public void print(){System.out.println("Hello");}

public static void main(String args[]){

A6 obj = new A6();

obj.print();

Output:

Hello

Java Interface Example: Drawable

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.

//Interface declaration

interface Drawable{

void draw();

}
//Implementation

class Rectangle implements Drawable{

public void draw(){System.out.println("drawing rectangle");}

class Circle implements Drawable{

public void draw(){System.out.println("drawing circle");}

//Using interface

class TestInterface1{

public static void main(String args[]){

Drawable d=new Circle();

d.draw();

}}

Output:

drawing circle

Multiple inheritance in Java by interface

If a class implements multiple interfaces, or an interface extends multiple interfaces, it is


known as multiple inheritance.

multiple inheritance in java

interface Printable{

void print();

interface Showable{

void show();

}
class A implements Printable,Showable{

public void print(){System.out.println("Hello");}

public void show(){System.out.println("Welcome");}

public static void main(String args[]){

A obj = new A();

obj.print();

obj.show();

Output

Hello

Welcome

Interface inheritance

A class implements an interface, but one interface extends another interface.

interface Printable{

void print();

interface Showable extends Printable{

void show();

class TestInterface implements Showable{

public void print(){System.out.println("Hello");}

public void show(){System.out.println("Welcome");}


public static void main(String args[]){

TestInterface obj = new TestInterface();

obj.print();

obj.show();

Output:

Hello

Welcome

Java 8 Default Method in Interface

Since Java 8, we can have method body in interface. But we need to make it default
method. Let's see an example:

interface Drawable{

void draw();

default void msg(){System.out.println("default method");}

class Rectangle implements Drawable{

public void draw(){System.out.println("drawing rectangle");}

class TestInterfaceDefault{

public static void main(String args[]){

Drawable d=new Rectangle();

d.draw();
d.msg();

}}

Output:

drawing rectangle

default method

Java 8 Static Method in Interface

Since Java 8, we can have static method in interface. Let's see an example:

interface Drawable{

void draw();

static int cube(int x){return x*x*x;}

class Rectangle implements Drawable{

public void draw(){System.out.println("drawing rectangle");}

class TestInterfaceStatic{

public static void main(String args[]){

Drawable d=new Rectangle();

d.draw();

System.out.println(Drawable.cube(3));

}}
Output:

drawing rectangle

27

What is marker or tagged interface?

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.

//How Serializable interface is written?

public interface Serializable{

Nested Interface in Java

Note: An interface can have another interface which is known as a nested interface. For
example:

interface printable{

void print();

interface MessagePrintable{

void msg();

You might also like