0% found this document useful (0 votes)
188 views15 pages

Abstract Classes and Interface

Abstraction allows representing concepts at a high level without including unnecessary details. It is a key feature of object-oriented programming that focuses on what objects do rather than how they work. Abstraction is achieved in Java through abstract classes and interfaces. Abstract classes can contain both abstract and concrete methods while interfaces contain only abstract methods. Subclasses must implement all abstract methods from the parent abstract class or interface.

Uploaded by

Supinder Sekhon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
188 views15 pages

Abstract Classes and Interface

Abstraction allows representing concepts at a high level without including unnecessary details. It is a key feature of object-oriented programming that focuses on what objects do rather than how they work. Abstraction is achieved in Java through abstract classes and interfaces. Abstract classes can contain both abstract and concrete methods while interfaces contain only abstract methods. Subclasses must implement all abstract methods from the parent abstract class or interface.

Uploaded by

Supinder Sekhon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Abstraction

A concept of representing something at high level, without


going into too much details.
An important feature of OOPS.
Abstraction is the quality of dealing with ideas rather than
events.
A process of hiding the complexity and implementation details
from the user.
Focuses on what the object does instead of how it does it.
In Java Abstraction is achieved using Abstract classes and
Interfaces.
An interface or abstract classes are not concrete, rather are
incomplete.
To use interface or abstract class, one needs to extend and

Abstract Class
An abstract class is a class that is declared abstract.
abstract keyword is used to create it.
Used to provide a template or design for concrete subclasses
down the inheritance tree.
Used to declare common characteristics of subclasses.
Can only be used as a superclass for other classes.
Cannot be instantiated.
A class with at least one abstract method(without body) must
be declared as abstract.
Abstract classes can also have normal methods with definitions
(also called concrete method), along with abstract methods.
After inheriting an abstract class, implementation has to be
provided to all the abstract methods in it.

Example of Abstract class


abstract class A
{
abstract void
callme();
}

class B extends A
{
void callme()
{ System.out.println("this is callme.");
}
public static void main(String[] args)
{ B b=new B();
b.callme();
}
}

Example Code
abstract class A
{
abstract void callme();
public void normal()
{
System.out.println("this is concrete
method");
}
}

class B extends A
{
void callme()
{
System.out.println("this is
callme.");
}
public static void main(String[]
args)
{
B b=new B();
b.callme();
b.normal();
}
}

Abstract Method
An abstract method is a method that is declared without an
implementation.
It just has a method signature.
Methods declared without any body within an abstract class.
The method body will be defined by its subclass.
Abstract method can never be final and static.
Any class that extends an abstract class must implement all
the abstract methods declared by the super class.
Syntax:
abstract return_type function_name ();

// No definition

When to use Abstract Method & Abstract


Class

Abstract methods:
Usually declared where two or more subclasses are expected
to do a similar thing in different ways through different
implementations.
The subclasses extend the same Abstract class and provide
different implementations for the abstract methods.
Abstract class:
Used to define generic types of behaviour of methods and
class hierarchy.
Uses its subclasses to provide implementation details of the
abstract class.

Example Code
public abstract class Person
//abstract class
{ private String name;
private String gender;
public Person(String nm, String gen)
{ this.name=nm;
this.gender=gen;
}
public abstract void work(); //abstract
method
} work() is an abstract method and it has no body.
that

public class Employee extends Person


{ private int empId;
public Employee(String nm, String gen, int id)
{ super(nm, gen);
Note
that
subclass
this.empId=id;
Employee inherits the
}
properties and methods
public void work()
of superclass Person.
{ if(empId == 0)
System.out.println("Not working");
else
System.out.println("Working as employee!!");
}
public static void main(String args[])
{ //coding in terms of abstract classes
Person student = new Employee("Dove","Female",0);
Person employee = new
Employee("Pankaj","Male",123);
student.work();

Example Code
abstract class Bank
{ abstract int getRateOfInt();
class SBI extends Bank
{ int getRateOfInt()
{ return 7;
}
}
class PNB extends Bank
{ int getRateOfInt()
{ return 7;
}
}

class TestBank
}
{ public static void main(String args[])
{ Bank b=new SBI();
int intrst=b.getRateOfInt();
System.out.println("Rate is: "+intrst+"
%");
}
}

Points to Remember
An abstract class must have an abstract method.
Abstract classes can have Constructors, Member variables and
Normal methods.
Abstract classes are never instantiated.
When you extend Abstract class with abstract method, you must
define the abstract method in the child class, or make the child
class abstract.

Interface

Can be defined as a generic template.


It can have methods and variables just like the class.
The methods declared in interface are by default abstract (only
method signature, no body).
The variables declared in an interface are public, static & final
by default.
Interface definition begins with a keyword interface.
An interface like that of an abstract class cannot be instantiated.
A class that implements an interface must define all the
methods of interface.
implements keyword is used to implement an interface.
An interface cant extend any class but can extend another
interface.
Java provides multiple inheritance in interfaces, that is an

Interface Implementation

interface MyInterface
{ public void method1();
public void method2();
}
class XYZ implements MyInterface
{ public void method1()
{ System.out.println("implementation of method1");
}
public void method2()
{ System.out.println("implementation of method2");
}
public static void main(String arg[])
{ MyInterface obj = new XYZ();
obj. method1();
}
}
Note: A class implements interface but an interface

Interface Implementation
An interface can not implement another interface.
It has to extend the other interface if required.
Example:
public interface Inf1
{ public void method1(); }
public interface Inf2 extends Inf1
{ public void method2(); }
public class Demo implements Inf2
{ public void method1()
{ System.out.println(This is the 1st method);
public void method2()
{ System.out.println(This is the 2nd method);
}

Example Code
public interface Shape
{ public String LABLE="Shape";
void draw();
}
public abstract class ShapeAbs
implements Shape
{ abstract public double getArea();

public class Circle extends ShapeAbs


{ private double radius;
double p=3.14;
public Circle(double r)
{
this.radius = r; }
} public void draw()
{ System.out.println("Drawing
Circle"); }
public double getArea()
{ return
p*this.radius*this.radius; }
public static void main(String[]
args)
{ ShapeAbs shape = new
Circle(10);
shape.draw();

Difference
Abstract Class
can extend only one class or one
abstract class at a time
can extend from a class or from an
abstract class
can have both abstract and
concrete methods
can extend only one abstract class
Keyword abstract is mandatory to
declare a method as an abstract
can have protected , public and
public abstract methods
can have static, final or static final
variable with any access specifier

Interface
can extend any number of
interfaces at a time
can extend only from an interface
can have only abstract methods
can implement any number of
interfaces
keyword abstract is optional to
declare a method as an abstract
can have only public abstract
methods by default
can have only static final variable
by default

You might also like