More Inheritance - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

More Inheritance

Description:

we can define a new class to inherit from an existing class ... private data and methods are inherited, but cannot be accessed directly; ... – PowerPoint PPT presentation

Number of Views:110
Avg rating:3.0/5.0
Slides: 24
Provided by: kenb97
Category:

less

Transcript and Presenter's Notes

Title: More Inheritance


1
More Inheritance
  • protected visibility
  • Overriding methods
  • Example Publications
  • Accessing Superclass constructors
  • the super keyword

2
Inheritance and Classification
  • we can define a new class to inherit from an
    existing class
  • all data and method definitions from the
    superclass are then inherited by the subclass
  • we also define new data and methods specially
    for the subclass
  • an object of the subclass is also an object of
    the superclass
  • inheritance allows effective re-use of existing
    class designs

3
Inheriting private members
CurrentAccount
private holder private number private balance
laserNumber
public getBalance() public toString() public
deposit(...) public withdraw(...)
public Account() public Account(...) public
Account(...)
getLaserNumber() payByCard(...)
4
Accessing private members of a superclass
  • //possible method for CurrentAccount (which
    extends Account)
  • //doesn't work, because balance is private to
    Account
  • public double payByCard(double sum)
  • if (sum gt balance)
  • balance balance - sum
  • return sum
  • else
  • return 0.00

that is why we issued a call to the public
method withdraw(...) in the original version
5
Protected members
  • If we want to make some variables and methods
    accessible to sub-classes, but not to other
    classes, we can declare them as protected.
  • For example, we might want to add a personal
    identification number for all accounts. This
    needs to be accessible inside
  • the subclass.
  • Solution declare the PIN as protected data,
    which can be used by the class and its subclasses
    only, and a public method changePIN which can be
    used anywhere, but
  • requires the current PIN as input.

6
public class Account ... protected int
pin //personal id number ... public
boolean changePIN(int oldPin, int newPin)
if (oldPin pin) pin newPin
return true else
return false
7
Access Modifiers definitions
  • private data and methods in an object can be
    accessed from methods declared in the same class,
    and cannot be accessed otherwise
  • protected data and methods in an object can be
    accessed from methods declared in the same class
    or its subclasses, and cannot be accessed
    otherwise
  • public data and methods in an object can be
    accessed by any object using any of its methods

8
Visibility summary
A directory
AClass
AnotherClass
YetAnotherClass
public var1 private var2 var3 protected var4
sees var1, var3
sees var1
ASubClass
AnotherSubClass
has and can see var1, var3, var4 has var2 but
cant see it
has and can see var1, var4 has var2,var3 but
cant see them
9
Overriding instance methods
  • we saw last lecture that a subclass inherits all
    the instance variables and instance methods of
    the superclass
  • what happens if you don't want all the instance
    methods (or at least, you don't want the
    implementation)?
  • override the method by supplying a new subclass
    method
  • must have same name, return type and signature
  • the superclass method can still be accessed (if
    visible) by prefixing with the super keyword
  • the subclass cannot offer fewer services
  • overriding method must be as visible or more
    visible (i.e. if superclass method is protected,
    subclass method can be protected or public, etc.)

10
Example Inheritance
Publication author title year pages getPages() toS
tring()
Book publisher getPublisher()
JournalPaper journal startPage getJournal()
11
Example (protected pages)
public class Publication protected int
pages public String toString() return "pp"
pages public class JournalPaper extends
Publication private int startPage
public String toString() return "pp"
startPage "--" (startPage
pages)
12
Example (private pages)
public class Publication private int
pages public int getPages() return pages
public String toString() return "pp"
pages public class JournalPaper extends
Publication private int startPage
public String toString() return "pp"
startPage "--" (startPage
getPages())
or return "pp" startpage ""
(startPage super.getpages())
13
Overriding vs overloading
  • Overloading
  • two methods in the same class with the same name,
    same return type, but different parameter lists
  • Overriding
  • Two methods, one in a subclass, with the same
    name, same return type, same parameter lists
  • Both overloading and overriding should be used
    sparingly.

14
Inheritance and overriding for re-use
  • suppose your project requires a class that is
    similar but not identical to one provided
    elsewhere (e.g. in a package)
  • define a subclass of the existing class
  • inherit what you like
  • override what you don't like
  • extend with additional methods and variables
  • you don't need the source code of the original
    class

15
Example Publication class
public class Publication private String
title private String author private int
year provate int pages public
Publication(String t, String a, int y, int p)
title t author a year y
pages p //and other
appropriate methods
16
How do we create subclass objects?
//create a new publication (perhaps a book?)
//title, author, year, num pages Publication p
new Publication("Java Grinds", "Smith",
2003, 357) //create a
new journal paper, //title, author, year, num
pages, journal, start page JournalPaper jp new
JournalPaper("Teaching Java",
"Smith", 2003, 23,
"CS Teaching", 671)
17
So the subclass Constructor is ...?
  • public class JournalPaper extends Publication
  • private String journal
  • private int startpage
  • public JournalPaper(String t, String a, int y,
    int p,
  • String j, int sp)
  • title t
  • author a
  • //...
  • journal j
  • startpage sp
  • //...

WRONG title is private in the superclass, so
cannot be accessed here.
18
... or maybe ...?
  • public class JournalPaper extends Publication
  • private String journal
  • private int startpage
  • public JournalPaper(String t, String a, int y,
    int p,
  • String j, int sp)
  • super.setTitle(t)
  • super.setAuthor(a)
  • //...
  • journal j
  • startpage sp
  • //...

We would have to know which of the superclass
fields needed to be initialised with sensible
values, and the superclass "set" methods must
exist.
In fact, Java won't let us do this ...
19
Constructors and Inheritance
  • Java requires us to call the superclass
    constructor explicitly
  • avoids problems of visibility and data
    inconsistency
  • The invocation of the superclass constructor uses
    the special method "super", which takes whatever
    arguments the superclass constructor required.
  • Java expects the super call to be the first
    statement inside a constructor. If it isn't, it
    inserts a call to the superclass no-argument
    constructor, ignoring any arguments that were
    passed in at the subclass level. If there is no
    such
  • superclass constructor, it reports a compiler
    error.

20
Correct use of superclass constructor
  • public class JournalPaper extends Publication
  • private String journal
  • private int startpage
  • public JournalPaper(String t, String a, int y,
    int p,
  • String j, int sp)
  • super(t, a, y, p)
  • //...
  • journal j
  • startpage sp
  • //...

the superclass constructor is part of its
interface, so we know what arguments to pass
21
Incorrect subclass constructor
  • public class JournalPaper extends Publication
  • ...
  • public JournalPaper(String t, String a, int y,
    int p,
  • String j, int sp)
  • journal j
  • startpage sp
  • ...

no call to the superclass, so title t, author a,
year y and start page sp are not passed up to
Publication(...)
22
Inheritance Summary
  • we can build class hierarchies using the keyword
    extends
  • each child (subclass) inherits all the data and
    methods of its parent (superclass)
  • private data and methods are inherited, but
    cannot be accessed directly protected data and
    methods can be accessed directly
  • constructor methods must be invoked in the first
    line in a subclass constructor as a call to super
  • inheritance allows us to re-use classes by
    specialising them

23
Next lecture ...
  • polymorphism
Write a Comment
User Comments (0)
About PowerShow.com