Download presentation
Presentation is loading. Please wait.
1
Inheritance
2
Inheritance It is a form of software reuse.
A new class is created by absorbing an existing class’ members, adding new attributes and methods, and overriding methods.
3
Inheritance Advantages Disadvantages Less development time and effort.
Ease of extending programs. Disadvantages It violates encapsulation since the implementation of the superclass and subclass become tightly coupled. New methods added to the superclass can break the subclass. Superclass and subclass need to evolve (i.e. develop) in parallel.
4
Inheritance Existing class New class
is called SUPERCLASS (a.k.a. parent or base). New class is called SUBCLASS (a.k.a. child or derived, can become the superclass for future subclasses). Normally adds its own fields and methods. Inherits all attributes and methods of the superclass.
5
Inheritance Direct Superclass Indirect Superclass
Is a superclass from which the subclass explicitly inherits. Indirect Superclass Is any class above the direct superclass in the class hierarchy.
6
Inheritance – Class Hierarchy consider the following objects:
Person Student Graduate Student Undergraduate Employee
7
Inheritance The arrows designates the is-a relationship. That is
An Employee is-a Person A Student is-a Person A Graduate Student is-a Student An Undergraduate is-a Student But the opposite is NOT necessarily true.
8
Person person = new Student(…);
Inheritance Each Inheritance should pass the is-a rule/relationship. Student is-a Person Hence, we can use superclass references (i.e. variables) to refer to instances of subclasses. Person person = new Student(…); This above statement is legal since a Student object has all the instance variables and methods of the Person class.
9
Student stud = new Person (…);
Inheritance On the other hand, is-a rule/relationship is not symmetric (i.e. not vice versa). Student stud = new Person (…); An object from the Person class is not guaranteed to have all the attributes and methods of the Student class.
10
Inheritance Suppose the Person class has instance variables
name age and instance methods getName getAge toString
11
Inheritance Then, every one of the derived classes inherits these variables and methods. The student class may have additional instance variables studentId and gpa, plus a method computeGrade(). And all of these additional features are inherited by the subclasses GraduateStudent and UnderStudent.
12
Inheritance Suppose GraduateStudent and UnderStudent use different algorithms for computing the course grade, the computeGrade implementation can be redefined in these classes and this is called method overriding.
13
Inheritance The extends keyword
used to specify the inheritance relationship between a subclass and a superclass.
14
do not inherit anything from each other
Inheritance Consider the following inheritance hierarchy Student Graduate Student Undergraduate Sibling classes do not inherit anything from each other
15
Inheritance The UnderGrad and GradStudent subclasses inherit all of the methods and variables of the Student superclass
16
Inheritance Sample classes using inheritance…
(Person-Student-StudentAthlete)
17
Inheritance private vs. protected
private instance variables not directly accessible to the methods in the subclass. Use accessor to access and modify instance variables of the superclass. (show the Student class with instance variable id which is declared as private)
18
Inheritance private vs. protected
Another reason for encapsulation is to protect your class against accidental or willful stupidity. A class often contains a number of interdependent fields that must be in a consistent state. If you allow a programmer (including yourself) to manipulate those fields directly, he might change one field without changing important related fields, thus leaving the class in an inconsistent state. If, instead, he has to call a method to change the field, that method can be sure to do everything necessary to keep the state consistent. Similarly, if a class defines certain methods for internal use only, hiding these methods prevents users of the class from calling them.
19
Inheritance private vs. protected
protected instance variables directly accessible to the methods in the subclass. A superclass protected members can be accessed by the following: members of that superclass members of its subclass members of other classes in the same package
20
Inheritance super keyword
Used to invoke the parent’s constructor. Used to invoke the parent’s version of an overridden method. This keyword is used for a subclass method wants to do what the superclass does, plus something extra. show the GradStud subclass whereby the computeGrade method of the superclass is invoked plus it adds another if statement condition
21
Inheritance Constructor
Constructors are never inherited. The subclass constructor may call the superclass constructor to instantiate the instance variables encapsulated in the superclass (e.g. GradStudent class). If no constructor is written for a subclass, the superclass default constructor with no parameters is generated. But if the superclass does not have a default (zero-parameter) constructor, but only a constructor with parameters, a compilation error will occur.
22
Inheritance Constructor
Java calls the constructor of the superclass first then calls the constructor of the subclass.
23
Inheritance Activity Create 3 classes 1st class - Person
Members: String name (initialize in a constructor) getName() toString() 2nd class - Stud int id (initialize in a constructor) toString() method
24
Inheritance Activity Create 3 classes 3rd class – GradStud
Members: toString() - call the following member methods: Test the class by creating an object of the GradStud class.
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.