0% found this document useful (0 votes)
2 views26 pages

PSOOP-Lecture 07-2024-25

The document discusses abstraction in Java, focusing on abstract classes and their role in hiding implementation details while providing essential functionality. It explains the concept of abstract methods, the rules governing abstract classes, and how subclasses must implement these methods or also be declared abstract. Additionally, it covers the use of final and static methods within abstract classes and provides examples to illustrate these concepts.

Uploaded by

dhruvvbhalani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views26 pages

PSOOP-Lecture 07-2024-25

The document discusses abstraction in Java, focusing on abstract classes and their role in hiding implementation details while providing essential functionality. It explains the concept of abstract methods, the rules governing abstract classes, and how subclasses must implement these methods or also be declared abstract. Additionally, it covers the use of final and static methods within abstract classes and provides examples to illustrate these concepts.

Uploaded by

dhruvvbhalani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

ABSTRACT CLASS IN JAVA

-Compiled by Nikahat Mulla


Agenda

• Abstraction
• Abstract class

2
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.
The Monopoly Property Example
• There are properties on a
monopoly board
• Railroads, Utilities, and Streets are kinds
of properties
Property

Street Railroad Utility


A getRent Behavior
• One behavior we want in
Property is the getRent method
• problem: How do I get the rent
of something that is “just a
Property”?

5
The Property class
public class Property {

private int cost;


private String name;
public int getRent() {
return hmmmmm?????;
}
}
Doesn’t seem like we have enough information to
get the rent if all we know is it is a Property.

6
Potential Solutions
1. Just leave it for the sub classes.
 Have each sub class define getRent()
2. Define getRent() in Property and simply
return -1.
 Sub classes override the method with more
meaningful behavior.

7
Solution 1: Leave it to the Sub - Classes
// no getRent() in Property
// Railroad and Utility DO have getRent() methods

public void printRents(Property[] props) {


for (Property p : props)
System.out.println(p.getRent());

Property[] props = new Property[2];


props[0] = new Railroad("NP", 200, 1);
props[1] = new Utility("Electric", 150, false);
printRents(props);

Clicker 1 - What is result of above code?


A. 200150 B. different every time
C. compile error D. Class Cast Exception
E. Null Pointer Exception
8
Solution 1: Leave it to the Sub - Classes
// no getRent() in Property
// Railroad and Utility DO have getRent() methods

public void printRents(Property[] props) {


for (Property p : props)
System.out.println(p.getRent());

Property[] props = new Property[2];


props[0] = new Railroad("NP", 200, 1);
props[1] = new Utility("Electric", 150, false);
printRents(props);

Clicker 1 - What is result of above code?


A. 200150 B. different every time
C. compile error D. Class Cast Exception
E. Null Pointer Exception
9
Solution 1 workaround: "Fix" by Casting
// no getRent() in Property
public void printRents(Property[] props) {
for (Property p : props) {
if (p instanceof Railroad)
System.out.println(((Railroad) p).getRent());
else if (p instanceof Utility)
System.out.println(((Utility) p).getRent());
else if (p instanceof Street)
System.out.println(((Street) p).getRent())
} // GACK!!!!
}
Property[] props= new Property[2];
props[0] = new Railroad("NP", 200, 1);
props[1] = new Utility("Electric", 150, false);
printRents( props);

What happens as we add more sub classes of Property?


10
What happens if one of the objects is just a Property?
Solution 2: Fix with Placeholder Return
// getRent() in Property returns -1

public void printRents(Property[] props) {


for(Property p : props)
System.out.println(p.getRent());
}
Property[] props= new Property[2];
props[0] = new Railroad("NP", 200, 1);
props[1] = new Utility("Electric", 150, false);
printRents( props);

What happens if sub classes don’t override


getRent()?
Is that a good answer? 11
A Better Fix
• We know we want to be able to find the rent
of objects that are instances of Property
• The problem is we don’t know how to do
that if all we know is it a Property
• Make getRent() an abstract method
• Java keyword

12
Making getRent Abstract
public class Property {

private int cost;


private String name;

public abstract int getRent();


// I know I want it.
// Just don’t know how, yet…

}
Methods that are declared abstract have no body,
an undefined behavior.

13
Problems with Abstract Methods
Given getRent() is now an abstract method
what is wrong with the following code?

Property p = new Property();


System.out.println(p.getRent());

If things can go wrong with a tool, provide


safeguards to prevent that from happening.
Undefined Behavior = Bad
• Not good to have undefined behaviors
• If a class has 1 or more abstract methods, the
class must also be declared abstract.
• version of Property shown would cause a
compile error
• Even if a class has zero abstract methods a
programmer can still choose to make it
abstract
• if it models some abstract thing
• is there anything that is just a “Mammal”?

15
Abstract Classes Rules
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 class cannot be instantiated.
-- Can still declare variables of this type
3. A subclass must implement all inherited abstract
methods or be abstract itself.

16
Abstract Classes
public abstract class Property {

private int cost;


private String name;

public abstract double getRent();


// I know I want it.
// Just don’t know how, yet…

}
// Other methods not shown

if a class is abstract the compiler will not allow


constructors of that class to be called
Property s = new Property(1, “XYZ”);
//syntax error
17
Abstract Classes
• In other words, you can’t create instances of objects
where the class type is an abstract class
• Prevents having an object with an undefined
behavior
• Why would you still want to have constructors in an
abstract class?
• To initialize data members of the class
through child class super constructor calls
• Object variables of classes that are abstract types
may still be declared
Property p; //okay

18
Sub Classes of Abstract Classes
• Classes that extend an abstract class must
provide a working version of any and 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

19
Implementing getRent()
public class Railroad extends Property {

private static int[] rents


= {25, 50, 100, 200};

private int numOtherRailroadsOwned;

public double getRent() {


return rents[numOtherRailroadsOwned];}

// other methods not shown


}

20
A Utility Class

21
Polymorphism in Action
// getRent() in Property is abstract

public void printRents(Property[] props) {


for (Property p : props)
System.out.println(p.getRent());
}

22
final and static methods in abstract
classes
• Concrete methods in abstract classes can include
'final' and 'static' methods.
• A final method in an abstract class cannot be
overridden by subclasses.
• - Ensures that the method's implementation remains
unchanged.
• - Useful for defining behavior that should be
consistent across subclasses.
• Example:
class Vehicle {
final void engineType() {
System.out.println('Petrol Engine');
}
}
final and static methods in abstract
classes
• Static methods in abstract classes belong to
the class, not instances.
• - Can be called without creating an object of
the class.
• - Cannot be overridden by subclasses.
• Example:
abstract class Vehicle {
static void displayType() {
System.out.println('Vehicle type');
}
}
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


Thank You

You might also like