0% found this document useful (0 votes)
7 views

PSOOP Lab 07 Abstract Classes 2024 25

The document discusses abstract classes in Java, focusing on their role in achieving abstraction and runtime polymorphism. It explains the concept of dynamic method dispatch, the purpose of abstract classes, and how they can be inherited and utilized in subclasses. Additionally, it outlines the rules and characteristics of abstract classes, including their inability to be instantiated directly and the requirement for subclasses to implement abstract methods.

Uploaded by

dhruvvbhalani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

PSOOP Lab 07 Abstract Classes 2024 25

The document discusses abstract classes in Java, focusing on their role in achieving abstraction and runtime polymorphism. It explains the concept of dynamic method dispatch, the purpose of abstract classes, and how they can be inherited and utilized in subclasses. Additionally, it outlines the rules and characteristics of abstract classes, including their inability to be instantiated directly and the requirement for subclasses to implement abstract methods.

Uploaded by

dhruvvbhalani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Lab 07: ABSTRACT CLASSES IN JAVA

-Compiled by Nikahat Mulla


Agenda

• Runtime Polymorphism
• Abstraction
• Abstract class

2
Recap – Dynamic Method Dispatch

• It is the mechanism by which a call to an overridden method is resolved at run time,


rather than compile time.

• A superclass reference variable can refer to a subclass object. Java uses this fact to
resolve calls to overridden methods at run time.
• When an overridden method is called through a superclass reference, Java determines
which version of that method to execute based upon the type of the object being referred
to at the time the call occurs.
• Thus, it is the type of the object being referred to (not the type of the reference variable)
that determines which version of an overridden method will be executed.

• Hence, if a superclass contains a method that is overridden by a subclass, then when


different types of objects are referred to through a superclass reference variable, different
versions of the method are executed.
Recap – Dynamic Method Dispatch[Run-time
Polymorphism]
Abstraction in Java
• Abstraction in Java is the process in which we only
show essential details/functionality to the user.
• The non-essential implementation details are not
displayed to the user.
• Real world examples:
• Television remote control, Car
• Abstraction can be achieved with either abstract
classes or interfaces.
Abstract classes
• Consider the term ‘Animal’
• The term in itself is abstract (generalized)
• For modeling such abstract concepts into
classes, Java provides the idea of an abstract
class
• An abstract class is created using the
keyword abstract.
abstract class Animal{
String name;
}
Abstract classes
• Now consider designing the class Animal
• When we think of a particular method of Animal,
makeSound(): Can we say at present what sound
does the animal make?
• No, because we do not know what animal it is yet.
• Such methods, whose behavior cannot be defined
are called abstract methods.
• Abstract classes may contain 0 or more abstract
methods.
• They may also contain concrete methods
Example
// Abstract class
abstract class Animal {
private String name;
// Abstract method
abstract void makeSound();
// Concrete method
void eat() {
System.out.println(name + " is eating...");
}
}
What purpose do abstract classes
have?
• Abstract classes can serve the purpose of
generalization, that is forming a common super
class for upcoming subclasses
• Assuming there were classes like Dog, Cat, etc., we
realise that we can keep on adding more such
“Animal”s.
• To model this, we can create a parent class and
keep the common properties (name) and common
methods (makeSound(),eat()) in Animal class and
make the concrete classes inherit from Animal.
Making it abstract would mean that we will not use
it directly but through the child classes.
Inheriting from abstract classes
• Once we inherit from Animal, the classes like
Dog, Cat etc. are well-defined animals with
known features and method makeSound()
can now be implemented
Class Dog inheriting from Animal
// Concrete class implementing abstract class
class Dog extends Animal {
// Implementation of abstract method
@Override
void makeSound() {
System.out.println("Woof!");
}
}
Can abstract classes have
constructors?
• Yes, for instantiating the data members of the
abstract class, an abstract class may add constructors
// Abstract class
abstract class Animal {
private String name;
// Constructor
Animal(String name) {
this.name = name;
}
// Abstract method
abstract void makeSound();
// Concrete method
void eat() {
System.out.println(name + " is eating...");
}
}
Can abstract classes have
constructors?
• The subclasses which derive from the
abstract class may then access the base class
constructors using super
// Concrete class implementing abstract class
class Dog extends Animal {
Dog(String name) {
super(name);
}

// Implementation of abstract method


@Override
void makeSound() {
System.out.println("Woof!");
}
}
Can abstract classes have
constructors?
// Main class to test
public class TestAbstraction {
public static void main(String[] args) {
Dog dog = new Dog("Buddy");
dog.makeSound(); // Output: Woof!
dog.eat(); // Output: Buddy is eating..
}
}
Can we create objects of abstract
classes?
• No, we cannot instantiate the reference of an
abstract class through its own constructor
• For example,
• Animal a=new Animal();//invalid
• However, we can create objects of the
concrete child classes to instantiate the
parent class reference.
• Animal a1=new Dog();//valid
• Animal a2=new Cat();//valid
Polymorphism in Action
// area() in Shape is abstract

public void printAreas(Animal[] animals) {


for (Animal a : animals)
System.out.println(a.makeSound());
}

16
Abstract Classes Safety
1. A class with one or more abstract methods must be declared
abstract.
- Syntax error if not done.
- Can still decide to make class abstract even if no abstract
methods.
2. Objects of an abstract type cannot be instantiated.
-- Can still declare variables of this type
3. A subclass must implement all inherited abstract methods or
be abstract itself.

17
Sub Classes of Abstract Classes
• Classes that extend an abstract class must
provide a working version of all abstract
methods from the parent class
• or they must be declared to be abstract as
well
• could still decide to keep a class abstract
regardless of status of abstract methods

18
Abstract Class Summary
• A class that is declared with abstract keyword

• May or may not include abstract methods


• Abstract methods do not have implementation (body)

• Cannot be instantiated, but it can be subclassed

• When an abstract class is subclassed, the subclass provides


implementations for all abstract methods in its parent class

• And, if it does not, the subclass must also be declared abstract


Problem Statement 1:
• Create a base class as a Vehicle. The Vehicle class has wheels, speed,
mileage,rpm as private data members and two abstract methods, spec() to set
the values for data members and display_stats() to display the values assigned.
Also, define a Also, define a concrete method getPerformanceScore() in the
Vehicle class that computes a performance score based on the formula:
• Performance Score=(Speed×Mileage)/RPM
• Create classes LMV(Light Motor Vehicle), HMV(Heavy Motor Vehicle)
derived from the Vehicle class. Include variables fuelType(Petrol, Diesel,
Electric) in LMV and loadCapacity ( Maximum weight it can carry (in tons)) in
HMV and override the abstract methods in these classes. Also have constructor
initializing the values to 0 as default. In main, create an array of references of
the base class and set it to the objects of the derived classes.
• Read data for n vehicle objects by creating objects from a choice based input
and find the top performing vehicle in the array.
Thank You

You might also like