0% found this document useful (0 votes)
1 views8 pages

Abstract Classes

Abstract classes in Java are declared with the abstract keyword and may contain abstract methods, which must be implemented in subclasses. They cannot be instantiated directly, and any class inheriting from an abstract class must provide implementations for all abstract methods. An example is provided with an Employee class and its Salary subclass demonstrating how to compute pay.

Uploaded by

davidalieze2023
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)
1 views8 pages

Abstract Classes

Abstract classes in Java are declared with the abstract keyword and may contain abstract methods, which must be implemented in subclasses. They cannot be instantiated directly, and any class inheriting from an abstract class must provide implementations for all abstract methods. An example is provided with an Employee class and its Salary subclass demonstrating how to compute pay.

Uploaded by

davidalieze2023
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/ 8

Abstract

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.

If a class is declared abstract, it cannot be instantiated.

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;

public abstract double computePay();


// Remainder of class definition
}
05/13/2025
How abstract classes cannot be
used
Using the method by creating an object will lead to an error.

public class AbstractDemo {


public static void main(String [] args) {
/* Following is not allowed and would raise error */
Employee e = new Employee("George W.", "Houston, TX",
43);
System.out.println("\n Call mailCheck using Employee
reference--");
}
05/13/2025
How to use abstract classes
public class Salary extends Employee {

private double salary; // Annual salary

public double computePay() {

System.out.println("Computing salary pay for " + getName());

return salary/52;

// Remainder of class definition

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

You might also like