0% found this document useful (0 votes)
43 views

SD Abstract Class & Interface PDF

This document discusses abstract classes and interfaces in Java. It defines abstraction as hiding implementation details and focusing on what an object does rather than how it does it. Abstract classes can contain both abstract and concrete methods, while interfaces contain only abstract methods. The document provides examples of using abstract classes and interfaces to achieve abstraction and multiple inheritance in Java. It also compares the key differences between abstract classes and interfaces.

Uploaded by

Rahju
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

SD Abstract Class & Interface PDF

This document discusses abstract classes and interfaces in Java. It defines abstraction as hiding implementation details and focusing on what an object does rather than how it does it. Abstract classes can contain both abstract and concrete methods, while interfaces contain only abstract methods. The document provides examples of using abstract classes and interfaces to achieve abstraction and multiple inheritance in Java. It also compares the key differences between abstract classes and interfaces.

Uploaded by

Rahju
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Abstract class & Interface

Dr Sujit Das
NIT Warangal
Abstraction
• It 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 with the abstract keyword is known as an


abstract class in Java. It can have abstract and non-abstract methods
(method with the body).
• It needs to be extended and its method implemented. It cannot be
instantiated.
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


Abstract class concept in Java
Geometry
Geometry

Circle Rectangle Ellipse


Rectangle
public class Circle extends Geomerty
Package myShape; {
public double r;
public abstract class Geometry public Circle()
{ {r=1.0;}
static final double PI=3.14; public Circle (double r){this.r=r;}
public abstract double area(); public double area(){return PI*r*r;}
public abstract double circumference(); public double circumference()
} {return 2*PI*r;}
}
public class Rectangle extends Geometry
{
protected double l,w;
public Rectangle ()
{l=0.0; w=0.0;}
public Rectangle (double l, double w)
{
This.l=l; this.w=w;
}
public double area()
{
return l*w;
}
public double circumference()
{
return 2*(l+w);
}
}
public class GeoDemo
{
public static void main(String args[])
{
Geometry [] geoobjects = new Geometry[3];
geoobjects[0]=new Circle (2.0);
geoobjects[1]=new Rectangle (1.0, 3.0);
geoobjects[2]=new Ellipse (4.0, 2.0);
Double totalarea=0;
for(int i=0; i<3;i++)
{
totalarea=totalarea+geoobjects[i].area();
}
System.out.println(“Total area=”+totalarea);
}
Example of Abstract class that has an abstract method

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();
}
}
Understanding the real scenario of Abstract class

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();//In a real scenario, object is provided throughmethod, e.
g., getShape() method
s.draw();
}
}
Another example of Abstract class in java
abstract class Bank{
abstract int getRateOfInterest();
}
class SBI extends Bank{
int getRateOfInterest(){return 7;}
}
class PNB extends Bank{
int getRateOfInterest(){return 8;}
}

class TestBank{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
b=new PNB();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
}}
Abstract class having constructor, data member and methods

//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();
}
}
Interface in Java
• 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.
• 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.
Internal addition by the compiler
The relationship between classes and interfaces
Java Interface Example
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();
}
}
Java Interface Example: Drawable

//Interface declaration: by first user


interface Drawable{
void draw();
}
//Implementation: by second user
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: by third user
class TestInterface1{
public static void main(String args[]){
Drawable d=new Circle();//In real scenario, object is provided by method e.g. getDrawable()
d.draw();
}}
Java Interface Example: Bank

interface Bank{
float rateOfInterest();
}
class SBI implements Bank{
public float rateOfInterest(){return 9.15f;}
}
class PNB implements Bank{
public float rateOfInterest(){return 9.7f;}
}
class TestInterface2{
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInterest());
}}
Multiple inheritance in Java by interface
Multiple inheritance in Java by interface

interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}

public static void main(String args[]){


A7 obj = new A7();
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 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.
Multiple inheritance

interface Printable{
void print();
}
interface Showable{
void print();
}

class TestInterface3 implements Printable, Showable{


public void print(){System.out.println("Hello");}
public static void main(String args[]){
TestInterface3 obj = new TestInterface3();
obj.print();
}
}
Interface inheritance

interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class TestInterface4 implements Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}

public static void main(String args[]){


TestInterface4 obj = new TestInterface4();
obj.print();
obj.show();
}
}
Difference between abstract class and interface
Abstract class Interface
1) Abstract class can have abstract and non- Interface can have only abstract methods.
abstract methods.

2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.

3) Abstract class can have final, non-final, static and Interface has only static and final variables.
non-static variables.

4) Abstract class can provide the implementation of Interface can't provide the implementation of abstract
interface. class.

5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.

6) An abstract class can extend another Java class and An interface can extend another Java interface only.
implement multiple Java interfaces.

7) An abstract class can be extended using keyword An interface can be implemented using keyword
"extends". "implements".

8) A Java abstract class can have class members like Members of a Java interface are public by default.
private, protected, etc.
Example of abstract class and interface in Java

//Creating interface that has 4 methods //Creating subclass of abstract class, now we need to pro
interface A{ vide the implementation of rest of the methods
void a();//bydefault, public and abstract class M extends B{
void b();
void c();
public void a(){System.out.println("I am a");}
void d(); public void b(){System.out.println("I am b");}
} public void d(){System.out.println("I am d");}
}
//Creating abstract class that provides t
he implementation of one method of A interface //Creating a test class that calls the methods of A interfa
abstract class B implements A{ ce
public void c(){System.out.println("I am C");}
}
class Test5{
public static void main(String args[]){
A a=new M();
a.a();
a.b();
a.c();
a.d();
}}

You might also like