Inheritance - PowerPoint PPT Presentation

About This Presentation
Title:

Inheritance

Description:

Subclass method may need to access super class method that is overridden ... 'super' provides a way of invoking the overridden base class method ... – PowerPoint PPT presentation

Number of Views:9
Avg rating:3.0/5.0
Slides: 10
Provided by: lakshmish
Learn more at: http://www.cs.uga.edu
Category:

less

Transcript and Presenter's Notes

Title: Inheritance


1
Inheritance
  • Lakshmish Ramaswamy

2
Method Overriding
  • A mechanism through which a subclass replace a
    method existing in its superclass
  • New method defn. in subclass must have same
    method signature (method name parameters) and
    same return
  • New defn. cannot narrow the accessibility, but
    can widen
  • New method defn. can only specify all or a subset
    of the exception classes specified by the throws
    clause of the overridden method
  • Cannot override final methods

3
Partial Inheritance and super
  • Subclass method may need to access super class
    method that is overridden
  • Doing a little extra from what the base class
    method does
  • Cant cut and paste the code due to accessibility
    restrictions
  • super provides a way of invoking the overridden
    base class method
  • Example toString method in Person and Student
    classes

4
Running Example
5
Naïve Implementation
6
Better Implementation with super
7
Constructors and super
  • Need to initialize all fields of subclass (even
    those declared in superclass)
  • What happens if superclass members are private?
  • Cannot be accessed by the subclass constructor
  • super provides a way of invoking superclass
    constructor from subclass constructor

8
Use of super in Constructor
9
Dynamic Dispatch Polymorhpism
  • If reference type and object being referenced
    have different implementation which one would be
    used
  • Reference type may be assigned to any subclass
    objects - Polymorphism
  • Actual object assignment may not be known until
    runtime
  • Dynamic dispatch Implementation of the dynamic
    type will be used
Write a Comment
User Comments (0)
About PowerShow.com