Abstract Classes and Interface in JAVA
Abstract Classes and Interface in JAVA
Interface in JAVA
Lecture 16 and 18
What is an abstraction
Ways to achieve abstraction
Abstract class
Interface
Difference between abstract and interface
References
Abstraction (1/7)
• Abstraction is a process by which concepts are derived from the
usage and classification of literal ("real" or "concrete") concepts.
• Abstraction is a concept that acts as a super-categorical noun for all
subordinate concepts, and connects any related concepts as a group,
field, or category.
• Abstractions may be formed by reducing the information content of a
concept or an observable phenomenon, typically to retain only
information which is relevant for a particular purpose.
• For example, abstracting a leather soccer ball to the more general
idea of a ball retains only the information on general ball attributes
and behavior, eliminating the other characteristics of that particular
ball.
Abstraction (2/7)
• Example Vehicle
Example Vehicle
• Here we have four classes Box truck, Classic
truck, Sports car and Sedan car.
Example Animal
Example
• Aircraft can be the
abstraction for a Propeller
plane, Fighter plane,
Passenger plane etc. It acts
as a super-categorical
noun.
• As an abstract concept it
Propeller plane Fighter plane Passenger plane
can hold all the common
properties and behaviors
present in different aircrafts.
Abstraction (7/7)
Aircraft
Example color :
String engine :
Object
start()
• Here we have the abstract class Aircraft. We stop()
have defined color and engine properties and takeOff()
land()
start, stop takeOff and land behaviors which
are present in all Aircrafts.
2. Interface (100%)
1. Abstract class
•It can have final methods which will force the subclass not to change the body
of the method.
Abstract class
Bank Account
deposit(amount : Dollar)
• Here we have two classes withdraw(amount : Dollar)
abstract class Bike
{
Bike() //constructor
{}
abstract void run(); //abstract method
Birds
Interface
<<IWritable>>
Example
draw()
Person
write(IWritable : instrument)
Interface
• An interface in Java is a blueprint of a class. It has static constants and abstract
methods.
• There can be only abstract methods in the Java interface, not method body.
• In other words, you can say that interfaces can have abstract methods and
variables.
It 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.
Interface (Syntax)
interface abc
{
// declare constant fields
// declare methods that abstract
// by default.
}
Interface fields are public, static and final by default, and the
methods are public and abstract.
Relationship b/w 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.
Interface (Example with code)
//Interface declaration: by first user
interface Drawable
{
void draw();
} //Main method
//Implementation: by second user public static void main(String args[])
class Rectangle implements Drawable {
{ Drawable d=new Circle;
public void draw() d.draw();
{System.out.println("drawing rectangle");} }
}
class Circle implements Drawable
{
public void draw()
{System.out.println("drawing circle");}
}
Interface (Example explanation)
The implementation part is hidden by the user who uses the interface.
When to use Interface?
• In such case, the end user may not be forced to override all the
methods of the interface.
Example
interface A
{
void a();
void b();
void c(); //Main method
void d(); public static void main(String args[])
} {
abstract class B implements A A a=new M();
{ a.a();
public void c() {System.out.println("I am c");} a.b();
} a.c();
a.d();
class M extends B }
{
public void a() {System.out.println("I am a");}
public void b() {System.out.println("I am b");}
public void d() {System.out.println("I am d");}
}
Conclusion of abstract class and interface
(important point)
• Abstract class is a concept and implementation gets completed when it is being realized
by a subclass.
• Abstract classes provide elements of both inheritance and interfaces (see example on
previous slide).
• Some or all members of the class might be unimplemented, and it is up to the inheriting
class to provide that implementation.
• Members that are implemented might still be overridden, and the inheriting class can still
implement additional interfaces or other functionality.
Difference between abstract class and interface
Feature Interface Abstract class
Multiple Inheritance A class may implement several A class may inherit only one
interfaces. abstract class.
Default An interface is purely abstract, it cannot An abstract class can provide
implementation provide any code, just the signature. complete, default code and/or
just the details that have to be
overridden.
Access modifiers An interface cannot have access modifiers An abstract class can contain
for the method, properties etc. Everything access modifiers for the
is assumed as public. methods, properties etc.
Core vs. Peripheral Interfaces are used to define the An abstract class defines the
peripheral abilities of a class. In other core identity of a class and there
words both Human and Vehicle can inherit it is used for related objects.
from a IMovable interface.
Homogeneity If various implementations only share If various implementations are of
method signatures then it is better to use the same kind and use common
Interfaces. behavior or status then abstract
class is better to use.
Difference between abstract class and interface
Fields and Constants No fields can be defined in interfaces An abstract class can have
fields and constants defined
“is-a” and "can-do" analogy
• https://www.javatpoint.com/abstract-class-in-java
• https://www.geeksforgeeks.org/abstract-classes-in-java/?ref=lbp
33
THANK YOU