10 - Lec - Inheritance
10 - Lec - Inheritance
Inheritance
Allow us to specify relationships between types
Abstraction, generalization, specification
There “is-a” relationship
Uses “extends” keyword
Abstraction –specifying the framework and hiding the implementation level information.
Abstraction reduces the complexity by hiding low-level details.
Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be
inherited by another class).
Generalization and Specification
int getCiteCount() {
…
}
}
Brainstorming
What are some other examples of possible inheritance hierarchies?
Person -> student, faculty…
Shape -> circle, triangle, rectangle…
Other examples???
Inheritance (continued)
//Syntax
modifier(s) class ClassName extends ExistingClassName
{
// data member and methods
}
Inheritance (example)
Overriding Methods
A subclass can override (redefine) the methods of the superclass
Objects of the subclass type will use the new method
Objects of the superclass type will use the original
Access Control and Inheritance
A derived class can access all the non-private members of its base class.
Thus base-class members that should not be accessible to the member functions of
derived classes should be declared private in the base class.
Inheritance(continued)
Inheritance
The mechanism that allows us to extend the definition of a class without making any
physical changes to the existing class is inheritance.
Inheritance lets you create new classes from existing classes. Any new class that you
create from an existing class is called a derived class; an existing class is called a base
class.
The inheritance relationship enables a derived class to inherit features from its base class.
Furthermore, the derived class can add new features of its own. Therefore, rather than
create completely new classes from scratch, you can take advantage of inheritance and
reduce software complexity.
Forms of Inheritance
Single Inheritance: It is the inheritance hierarchy wherein one derived
class inherits from one base class.
Forms of Inheritance(Single Inheritance)
Parent1.java
Test.java
Output
Forms of Inheritance
Multiple Inheritance: It is the inheritance hierarchy wherein one derived class
inherits from multiple base class(es)
Forms of Inheritance
Hierarchical Inheritance: It is the inheritance hierarchy wherein multiple
subclasses inherit from one base class.
Forms of Inheritance: Hierarchical Inheritance
class A {
public void methodA() {
System.out.println("method of Class A");
}
}
class B extends A {
public void methodB() {
System.out.println("method of Class B");
}
}
class C extends A {
public void methodC() {
System.out.println("method of Class C");
}
}
class D extends A {
public void methodD() {
System.out.println("method of Class D");
}
}
Forms of Inheritance: Hierarchical Inheritance
class JavaExample {
public static void main(String args[]) {
B obj1 = new B();
C obj2 = new C();
D obj3 = new D();
Multilevel Inheritance:
Shape
Rectangle
Cube
Output
Forms of Inheritance
Hybrid Inheritance: The inheritance hierarchy that reflects any legal combination of
the other four types of inheritance.
It seems that because of this diagram, people are finding it difficult to understand this
topic because this diagram shows the combination of hierarchical and multiple
inheritance and multiple inheritance is not supported in java.
Forms of Inheritance
Hybrid Inheritance: The inheritance hierarchy that reflects any legal combination of
the other four types of inheritance.