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

10 - Lec - Inheritance

The document discusses inheritance in Java, including allowing relationships between types through 'is-a' relationships and the 'extends' keyword. It covers code reuse through inheritance and subclasses specializing by adding members or overriding methods. It also discusses abstraction, generalization, specification, and different forms of inheritance like single, multilevel, hierarchical and hybrid inheritance.

Uploaded by

Aqeel Abbas
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)
12 views26 pages

10 - Lec - Inheritance

The document discusses inheritance in Java, including allowing relationships between types through 'is-a' relationships and the 'extends' keyword. It covers code reuse through inheritance and subclasses specializing by adding members or overriding methods. It also discusses abstraction, generalization, specification, and different forms of inheritance like single, multilevel, hierarchical and hybrid inheritance.

Uploaded by

Aqeel Abbas
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/ 26

Inheritance in Java

Inheritance
 Allow us to specify relationships between types
 Abstraction, generalization, specification
 There “is-a” relationship
 Uses “extends” keyword

 Why is this useful in programming?


 Allows for code reuse
 More intuitive/expressive code
Code Reuse

 General functionality can be written once and applied to *any* subclass

 Subclasses can specialize by adding members and methods or overriding functions


Abstraction

 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

 Generalization – is the process of extracting shared characteristics from two


or more classes and combining them into a generalized superclass. Shared
characteristics can be attributes, associations, or methods.

 Specification – specification or specialization means creating new subclasses


from an existing class.
Generalization and Specification (Example)
Inheritance: Adding Functionality
 Subclasses have all of the data members and methods of the superclass

 Subclasses can have additional items


 Additional data members
 Additional methods

 Subclasses are more specific and have more functionality

 Superclasses capture generic functionality common across many types of objects


public class Person {
String name;
char gender;
Date birthday;

int getAge(Date today) {



}
}

public class Student extends Person


{
char[] grades;

double getGPA() { public class Professor extends


… Person
} {
} String[] researchPapers;

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();

//All classes can access the method of class A


obj1.methodA();
obj2.methodA();
obj3.methodA();
}
}
Forms of Inheritance

 Multilevel Inheritance:

 It is the inheritance hierarchy


where a subclass acts as a base
class for other classes.
Forms of Inheritance(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.

 Java Support these type of Hybrid inheritance


class Shape (example)

You might also like