0% found this document useful (0 votes)
0 views14 pages

Lecture-21- Abstract Classes and Methods (1)

The document discusses abstraction in programming, particularly in Java, highlighting its purpose of simplifying complex systems by hiding implementation details. It explains the use of abstract classes and methods, emphasizing their role in creating a common structure for subclasses and enforcing method implementation. Benefits of abstraction include increased code reusability, maintainability, and security.

Uploaded by

awaismumtaz1406
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)
0 views14 pages

Lecture-21- Abstract Classes and Methods (1)

The document discusses abstraction in programming, particularly in Java, highlighting its purpose of simplifying complex systems by hiding implementation details. It explains the use of abstract classes and methods, emphasizing their role in creating a common structure for subclasses and enforcing method implementation. Benefits of abstraction include increased code reusability, maintainability, and security.

Uploaded by

awaismumtaz1406
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/ 14

Lecture – 21

Abstract Classes and Methods


Abstraction
• Abstraction is the process of hiding the internal
implementation details and showing only the necessary
features of an object. It helps reduce complexity and allows the
programmer to focus on interactions at a higher level.
Purpose:
• It enables programmers to handle complexity by providing
simplified models of real-world entities.

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

• Enhances code maintainability

• Improves readability and usability

• Helps in security by hiding sensitive information

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

• An abstract class can contain abstract methods, which are


implemented in concrete subclasses. 4-7
Person

Abstract Classes
• Why we need abstract classes
– To make class generalize Faculty Student

• Abstract classes help define a generic structure that can be shared


among multiple related subclasses.
• To enforce a contract for subclasses
• Abstract methods (methods without implementation) must be
overridden by derived classes, ensuring consistency across them.
– Don’t want a class whose object can be created
– Person Class object is meaning less but Faculty and Student Objects
are requirement of School Mgmt. System.
– So Person Class is abstract class 4-8
Abstract Class
• Java abstract classes are use to declare common characteristics
of subclasses
• It can only be used as superclass for other classes that extend
the abstract class
• Like other classes, an abstract class can contain fields and
methods
• Can not create object of abstract classes but we can create
reference variable of abstract class
Person p = new Student();
4-9
Abstract Methods
• An abstract class can include methods that contain no
implementation
• These are called abstract methods
• The abstract method declaration must then end with a
semicolon rather than block
abstract void show();
• If a class has any abstract methods, whether declared or
inherited, the entire class must be declared abstract
4-10
What is wrong with the code?
abstract class Person{
Person class must be abstract because it contains
abstract void show(); abstract method
}
abstract class Student extends Person{ Since student class inherit method abstract void
… show(). Since it is still abstract so that’s why the
} student class must be abstract.
class Main{
public static void main(String[] args){ Since Student class has become abstract. Then it
Student s = new Student(); can not be instantiated,
}
}

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

You might also like