Lecture-21- Abstract Classes and Methods (1)
Lecture-21- Abstract Classes and Methods (1)
4-2
Abstraction
• It enables programmers to handle complexity by providing
simplified models of real-world entities.
Example:
• When a driver presses the brake pedal, they don’t need to
know how the brake system works internally (hydraulics, disc
pressure, etc.). They only interact with the brake interface (the
pedal), not the internal mechanism.
4-3
Benefits of Abstraction:
• Increases code reusability
4-4
How Abstraction is Achieved in Java
• Java supports abstraction through two main mechanisms:
Abstract Classes:
• Used when classes share common behavior, but also have
some methods that must be implemented by subclasses.
Interfaces:
• Used to achieve 100% abstraction. Interfaces define a contract
that classes agree to follow, without enforcing how methods
are implemented.
4-5
Abstract Class
• An abstract class is a class that cannot be instantiated and is meant to
be inherited by other classes.
• It is used to provide a common definition for a base class that multiple
derived classes can share.
• A class that has at least one abstract method is called an abstract class
• Abstract methods have no body and must be implemented by
subclasses.
– An abstract class must have the modifier abstract included in its class
heading:
public abstract class Employee
{
private instanceVariables;
. . .
public abstract double earning();
. . .
} 8-6
Abstract Classes
• Abstract classes can not be instantiated
• Abstract classes are declared with the keyword abstract
abstract class Person{ public class Main{
private String name; public static void main(String args[]){
private int age; // ❌ This line will cause a compile-time error
// Regular (concrete) method Person p = new Person();
public void setName(String n) }
{ name = n;} }
public void setAge(int a)
{ age = a;} Person is abstract; cannot be
} instantiated
Abstract Classes
• Why we need abstract classes
– To make class generalize Faculty Student
4-11
abstract class Person{
Since Student class is overriding the show()
abstract void show(); method then Student class no more abstract.
}
class Student extends Person{ In main we can make its object.
void show(){//some code}
}
class Main{
public static void main(String[] args){
Student s = new Student();
s.show();
}
}
4-12
Why abstract methods?
• When a method don’t require implementation for its
declaration
• Any class that contains an abstract method should be declared
as an abstract class. Although the opposite might not be true
i.e. it is not necessary that an abstract class should have an
abstract method.
• Inheritance of an abstract class by a regular class requires the
implementation of all the abstract methods in the parent class.
4-13
Points about abstract classes
• Abstract methods may or may not be present in the Java abstract class.
• The presence of at least one abstract method in a class makes the class an abstract
class.
• An abstract class cannot have any objects and therefore cannot be directly
instantiated.
• An abstract class can be used only if it is inherited from another class and
implements the abstract methods.
• Proper implementations of the abstract methods are required while inheriting an
abstract class.
• Parameterized constructors may be present in an abstract class. Also, an abstract
class always contains a default constructor.
4-14