Inheritance and Polymorphism Presentation
Inheritance and Polymorphism Presentation
class Base {
final void display() {
System.out.println("Final method");
}
}
// class Derived extends Base {
// void display() { } // Error: cannot override final method
// }
The Object Class
Every class in Java implicitly inherits from java.lang.Object.
Common methods:
- toString()
- equals(Object obj)
- hashCode()
- getClass()
- clone()
Example:
class Person {
public String toString() {
return "Person class";
}
}
Introduction to Polymorphism
• Polymorphism means "many forms".
• Allows one interface to be used for a general
class of actions.
Two types:
- Compile-time (method overloading)
- Runtime (method overriding)
Dynamic Binding