Abstract Classes
Abstract Classes
Classes
BY DR. AJK
Abstract classes and guidelines
A Java class which contains the abstract keyword in its declaration is known as abstract
class.
Java abstract classes may or may not contain abstract methods, i.e., methods without
body ( public void get(); )
But, if a class has at least one abstract method, then the class must be declared abstract.
To use an abstract class, you have to inherit it from another class, provide
implementations to the abstract methods in it.
If you inherit an abstract class, you have to provide implementations to all the abstract
methods in it.
05/13/2025
Abstract class example
public abstract class Employee {
private String name;
private String address;
private int number;
return salary/52;
05/13/2025
How to use abstract classes
public class AbstractDemo {
public static void main(String [] args) {
Salary s = new Salary("Mohd Mohtashim", "Ambehta,
UP", 3, 3600.00);
System.out.println("salary: " + s.computePay());
}
}
05/13/2025
THANK YOU
FOR LISTENING
05/13/2025
Exercise
1. When is abstract classes applicable?
2. Write an abstract class for a student. The student should have
the following attributes: name, sex, reg no and matric no. The
class should have the following methods: getters and setters
for all the attributes. Then create a new and undergraduate
class to inherit the student class.
05/13/2025