Abstract Classes and Interface
Abstract Classes and Interface
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.
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
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
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
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();
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