EMP132-Java Programming-Basic
Abstraction in Java
Abstract classes and methods
A method with without body (no implementation) is known as abstract method. A method must always
be declared in an abstract class, or in other words you can say that if a class has an abstract method, it
should be declared abstract as well.
Ways to achieve Abstraction
There are two ways to achieve abstraction in java
• Abstract class (0 to 100%)
• Interface (100%)
Points to Remember
• Abstract classes are not Interfaces. They are different, we will study this when we will study
Interfaces.
• An abstract class may or may not have an abstract method. But if any class has even a single
abstract method, then it must be declared abstract.
• 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.
abstract class in Java
A class that is declared as abstract is known as abstract class. It needs to be extended and its method
implemented. It cannot be instantiated.
Example abstract class
abstract class A{}
abstract method in Java
A method that is declared as abstract and does not have implementation is known as abstract method.
Example abstract method
abstract void methodNametus(); //no body and abstract
1
TT@GU
EMP132-Java Programming-Basic
Example of abstract class that has abstract method
In this example, Bike the abstract class that contains only one abstract method run. It implementation is
provided by the Honda class.
abstract class Bike { //abstract class
abstract void run(); //abstract method
}
class Yamaha extends Bike {
void run() //implementation of abstract method
{
System.out.println("running safely..");
}
}
Important Points
• Abstract class having constructor, data member, methods etc.
• If there is any abstract method in a class, that class must be abstract.
• If you are extending any abstract class that have abstract method, you must either provide the
implementation of the method or make this class abstract.
2
TT@GU