0% found this document useful (0 votes)
10 views17 pages

Polymorphism Lec 12

This document discusses polymorphism and inheritance in object-oriented programming. It explains that polymorphism allows objects of different classes to be treated as objects of a common superclass through method overriding. The @Override directive ensures subclass methods match superclass signatures exactly. Abstract classes define generalized structures without implementing every method, requiring subclasses to implement abstract methods. The final keyword prevents overriding or subclassing to ensure consistent behavior.

Uploaded by

Arfaat Sanitary
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)
10 views17 pages

Polymorphism Lec 12

This document discusses polymorphism and inheritance in object-oriented programming. It explains that polymorphism allows objects of different classes to be treated as objects of a common superclass through method overriding. The @Override directive ensures subclass methods match superclass signatures exactly. Abstract classes define generalized structures without implementing every method, requiring subclasses to implement abstract methods. The final keyword prevents overriding or subclassing to ensure consistent behavior.

Uploaded by

Arfaat Sanitary
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/ 17

Further Details into

Polymorphsim
Recap
• Polymorphism allows objects of different classes to be treated as
objects of a common superclass
• Method overriding helps in this by Dynamic Method Dispatch
• Allows code to be written at super class level without knows the
implementation of subclass

Q When overridden method is called will the super class method be


called or the child class one?
Q When not overridden which method will be called
@Overide Directive
• @Override a method in a class Animal {
subclass intended to override a void makeSound() {
method of superclass. System.out.println("Some sound");
}
• Compiler directive to ensure that }
the method signature of subclass
and superclass matches 100% class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Bark");
}
}
Abstract Class
• A superclass that declares the structure without implementation
of every method
• defines generalized form shared by its subclasses
• Defines the nature of methods that subclasses must implement.

• Example: JComponent paintComponent method


• Abstract methods ensure that subclasses override all necessary
methods for meaningful implementation.

• Example
• Parent Class: Shape with a method area
• Child Classes: Rectangle, Square, Triangle etc can override them according to their
needs.
Can not be instantiated

General methods have


implementations that are
inherited by subclasses
Example
Subclass must provide
implementation
Example 2

A subclass square can


similarly have its own
implementation of area
Using final with Inheritance
Preventing Overriding Methods
• Overriding a very powerful tool but sometimes it is not desirable
• `final` keyword indicates that the method cannot be overridden by
subclasses.
• The `Object` has methods that are `final.
• critical to the consistent state of the object.
Toy Example
public class A {
public final void foo() {
System.out.println("A.foo()");
}
}

public class B extends A {


public void foo() { // compilation error
System.out.println("B.foo()");
}
}
Benefits of This
• Performance enhancement.
• The compiler can inline calls to
final methods ONLY
• Inlining  copying bytecode
directly into the calling function
• Normal Method resolved at
runtime, i.e. late binding.
• Final methods resolved at
compile time, i.e. early binding.
Preventing Class Inheritance
• Prevent a class from being inherited use the final keyword before
the class declaration.
• This signifies that the class is final and cannot be subclassed
Why?

• Fundamental and immutable


classes are generally final
• java.lang.String class in
Java.
• API Stability leading to
interoperability
• Performance optimization
because allows inlining
• A general convention
Access modifiers (public, private, protected) come before Non Access
modifiers (final, static etc)

You might also like