Outline - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Outline

Description:

extends Corvette. public Roof getRoof() { return new ConvertibleRoof ... as (assigned to, passed as a parameter, etc) a Corvette variable. X derived from Y ... – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 22
Provided by: cs11
Category:
Tags: corvette | outline

less

Transcript and Presenter's Notes

Title: Outline


1
Outline
  • Inheritance
  • Overriding Methods
  • Keywords
  • protected
  • super
  • final
  • References revisited
  • Composition vs. Inheritance

2
Recap
  • this keyword
  • Java Packages
  • Java class library

3
Inheritance
  • Derive new classes from old classes
  • Improve code re-use
  • Easier to manage and understand complexity
  • Syntax class extends

4
Super and Sub classes
  • The class being extended is called the
    superclass, or parent class
  • The class extending the superclass is the
    subclass, or derived class
  • Can be many levels of super/subclassespublic
    class B extends A public class C extends B
    public class D extends C public class
    C2 extends B etc

5
What Inheritance Means
  • All public data members and methods (except
    constructors) in the superclass are inherited by
    the subclass. It is as if their definitions are
    copied into the subclasss class definition.
  • Improves code re-usability- change need only be
    made once, if necessary
  • No members of the subclass are visible to the
    superclass

6
Inheritance Example
  • public class Person
  • public String name
  • public int age
  • public Date birthDay
  • public Person(String name, int age, Date
    birthDay)
  • this.name name
  • this.age age
  • this.birthDay birthDay
  • public void setName(String name)
  • this.name name

7
Example, cont
  • public class Student extends Person
  • public int studentID
  • public String dept
  • private float GPA
  • public Student(String name, int age, Date
    birthDay, int studentID,
  • String dept, float GPA)
  • super(name, age, birthDay)
  • this.studentID studentID

8
Over-riding Methods
  • Redefine methods inherited from superclass to add
    or change functionality
  • Onion-like view of an object
  • each level of inheritance is a skin on the
    onion
  • interacting with the object uses the outermost
    skin on the object a method call uses the
    definition in the most derived class

9
Method Over-riding Example
  • public class Corvette
  • public Roof getRoof() return new
    MetallicRoof()
  • public class ConvertibleCorvetteextends Corvette
  • public Roof getRoof() return new
    ConvertibleRoof()
  • public void retractRoof()
  • public void extendRoof()

10
Protected
  • Any protected member can be seen by a subclass
  • Protected members cannot be seen by unrelated
    classes
  • Only class members (variables and methods) can be
    protected (classes themselves cannot be
    protected).

11
super Keyword
  • Use super to refer to members of the current
    objects superclass.
  • Most often used for accessing an inner layer of
    the onion I.e., for calling the superclass
    version of a method which the subclass has
    over-ridden.
  • public Roof getRoof()
  • if ( convertibleRoofIsBroken ) return
    super.getRoof()
  • return new ConvertibleRoof()

12
super, cont
  • Can be used in a constructor to access a
    super-class constructor
  • Must be first line in constructor if present
  • Syntax super()
  • public class Student extends Person
  • public Student(String name, int age, Date
    birthday, int studentID)
  • super(name, age, birthday)
  • this.studentID studentID

13
final, revisited
  • For variables, final means unchangeable
    (constants)
  • A final class cannot be extended from
  • A final method cannot be over-ridden by a
    sub-class
  • Used for security purposes

14
References, revisited
  • A reference to an object of some derived class
    can be used as a reference of its classs
    superclass.
  • E.g. a ConvertibleCorvette variable can be used
    as (assigned to, passed as a parameter, etc) a
    Corvette variable.
  • X derived from YX is a Yso X can be used
    anywhere Y can

15
References, cont
  • The reverse is not necessarily true!!
  • public class A
  • public class B extends A
  • public class C extends A
  • An instance of A is not an instance of B a
    reference to an A may happen to be actually a
    reference to a B, but that is not true in general
    (in fact it could be a reference to a C, or just
    an A, as well).

16
Reference Examples
  • A a1 new A()
  • B b1 new B()
  • A a2 b1
  • A a3 new C()
  • B b2 a1 // invalid!
  • C c1 a3 // invalid!

17
Object References
  • All classes in Java that do not extend any other
    classes automatically extend class Object
  • An Object reference can therefore refer to any
    instance of any Object
  • Variables of type Object are essentially the same
    as (in C) void pointers
  • Primitive data types have no class, and are not
    Objects

18
Example Class Hierarchy

Object
Student
Person
Math
Employee
System
Container
Panel
Applet
Component
Button
19
Composition vs. Inheritance
  • Composition relationship has a
  • Inheritance relationship is a
  • E.g., a Student is a Person, and so should
    probably inherit from Person
  • A House has an owner, which is likely a person,
    but the house isnt a person this is a
    composition relationship
  • Composition relationships imply instance variables

20
Summary
  • Inheritance
  • Keywords extends, protected, super, final
  • Method Overriding
  • Superclass/subclass references
  • Object references
  • Composition vs. Inheritance

21
Next Class
  • Polymorphism in Java
  • MP4 is released
Write a Comment
User Comments (0)
About PowerShow.com