Docsity

Inheritance - Introduction to Computer Science - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Inheritance - Introduction to Computer Science - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Computer Science

Inheritance

Road Map

• Inheritance

– Class hierarchy

– Overriding methods

– Constructors

– Modifier: protected

– Keyword: super

• Reading:

– Liang 5: 8.1 – 8.

– Liang 6: 9.1 – 9.

Introduction to Inheritance

  • Inheritance is an object oriented construct which

promotes code reuse by allowing you to create a new

class using an existing classes as a “starting point”.

  • You simply state which class you want to inherit from in order

to gain its members (fields and methods).

  • You can add or modify its members once you have inherited

them.

  • Terminology
    • Superclass (parent class, base class)
      • The original class which has its members inherited
      • Typically, this is the more general class
    • Subclass (child class, derived class)
      • The class which inherits members from the superclass
      • In this class, we can specialize the general class by modifying and adding functionality.

Inheritance

  • Abstraction
    • Focus on commonalities among objects in system
  • “is-a” vs. “has-a”
    • “is-a”
      • Inheritance
      • subclass object treated as superclass object
      • Example: Car is a vehicle
        • Vehicle properties/behaviors also car properties/behaviors
    • “has-a”
      • Composition
      • Object contains one or more objects of other classes as members
      • Example: Car has a steering wheel

9.2 Superclasses and Subclasses (Cont.)

  • Inheritance examples

Superclass Subclasses Student (^) GraduateStudent, UndergraduateStudent Shape (^) Circle, Triangle, Rectangle Loan (^) CarLoan, HomeImprovementLoan, MortgageLoan Employee (^) Faculty, Staff BankAccount (^) CheckingAccount, SavingsAccount Fig. 9.1 Inheritance examples.

9.2 Superclasses and Subclasses (Cont.)

  • Inheritance hierarchy
    • Inheritance relationships: tree-like hierarchy structure
    • Each class becomes
      • superclass
        • Supply data/behaviors to other classes AND / OR
      • subclass
        • Inherit data/behaviors from other classes

Fig. 9.3 Inheritance hierarchy for Shapes.

Shape

TwoDimensionalShape ThreeDimensionalShape

Circle Square Triangle Sphere Cube Tetrahedron

Inheritance in Java

  • The keyword extends is used in Java for inheritance.
  • A class that extends another class is said to be a

subclass. It is also known as a child class or a derived

class.

  • The class it extends is called a superclass. Sometimes

the superclass is called a base class or a parent class.

  • A subclass can also modify its method members. This is

referred to as overriding.

  • A subclass inherits the non-private members of it’s

superclass.

  • Syntax:

public class Student extends CommunityMember

Keyword super

• The keyword super can be used in a subclass

to refer to members of the subclass’ superclass.

It is used in two ways:

  • To invoke a constructor of the superclass (more about

this on the next slide). For example (note: the

keyword new is not used):

  • super ();
  • super (params);
  • To call a superclass’ method (it is only necessary to

do so when you override (more on overriding soon

too) the method). For example:

  • super.superclassMethodName();
  • super.superclassMethodName(params);

Inheritance and constructors

  • Constructors do not get inherited to a subclass. Instead, the

subclass’ constructor automatically calls it’s superclass’ zero

parameter constructor before it executes any of the code in

it’s own constructor.

  • If you do not want to use the parent’s zero parameter

constructor, you must explicitly invoke one of the

superclass’ other constructors with the keyword super.

  • If you do this, the call to the superclass’ constructor must

be the first line of code in the subclass’ constructor. e.g:

Student (String name) {

super (?params?);

  • If the superclass has no zero parameter constructor, the first

line of any subclass constructor MUST explicitly invoke

another superclass constructor.