CIS 234: More on Inheritance - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

CIS 234: More on Inheritance

Description:

Overriding Methods ... can't override private methods in a superclass (get compiler error message) ... Why? What does 'overriding' mean? ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 18
Provided by: CBA
Category:

less

Transcript and Presenter's Notes

Title: CIS 234: More on Inheritance


1
CIS 234 More on Inheritance
  • Dr. Ralph D. Westfall
  • May, 2010

2
Sample Code, Etc.
  • use code in the Animal.zip file for demonstrating
    this in Eclipse
  • see Eclipse Tips (Debugging) for how to
    configure Eclipse to Step Into different classes

3
Constructors Review
  • a constructor is a method that runs when an
    object is created from a class
  • constructor has same name as the class
  • constructors can have arguments
  • constructor may initialize the object with
    variable values and/or execute other code
  • a class can have more than 1 constructor, to
    initialize more/different values

4
Constructors Review - 2
  • public class MyClass
  • private int myVariable
  • MyClass(int number) // same name
  • myVariable number)
  • // initializing with 1 argument
  • MyClass(int num, boolean ok)
  • // same name, different arguments

5
Subclasses and Constructors
  • a parent and a child class can each have their
    own constructors
  • both will run when a subclass object is created
  • can use subclass constructors to add additional
    variables that only the subclass needs

6
Superclass Arguments
  • if a superclass constructor needs arguments, the
    subclass must have a constructor (no longer
    optional)
  • subclass constructor must include a call to
    superclass constructor with its arguments
  • call to superclass constructor must be the very
    1st code in the subclass constructor
  • use keyword super, not superclass name
  • super(arg1, etc.)

7
Superclass Arguments - 2
  • public class OtherClass extends MyClass
  • OtherClass(int num)
  • super(num)
  • // num goes to MyClass constructor

8
Calling Superclass Methods
  • keyword super is a synonym for the actual name of
    the superclass
  • super(arg1, etc.) is same as calling the
    superclass constructor method by its name
  • super. (note . in front of method name) calls
    that method in the superclass
  • super.toString()
  • only need this if toString is in both classes

9
Access Modifiers Review
  • modifiers control which classes can use data and
    methods in another class
  • public any class can use
  • protected subclasses can use
  • private no other class can use
  • "friendly" is default if none of others used
  • can be used by classes that have been identified
    as being in same "package"

10
Information Hiding
  • data in a class should only be accessible to
    other classes via methods
  • avoids bugs other programs can't change data
    values in unexpected ways, or use data before it
    is ready to use
  • declare variables as private
  • programmer chooses which data to make accessible
    by providing public get and set methods

11
Information Hiding - 2
  • private data fields are not directly accessible
    within subclasses
  • two ways to make private superclass data
    accessible to its subclasses
  • declare as protected, rather than private
  • use get or set methods in superclass that have
    been declared as public

12
Using Parent Class Methods
  • a subclass can access public methods in parent
    class (e.g., gets and sets)
  • subclass can't directly access private methods in
    parent class
  • but can access public methods in the parent
    class, which can then call its own private methods

13
Overriding Methods
  • method in subclass with same name can override
    some superclass methods
  • can't override private methods in a superclass
    (get compiler error message)

14
Overriding Methods - 2
  • can put super. (super dot) in front of a method
    name to call that method in the parent instead of
    method in the subclass with the same name
  • not always necessary but is a good idea
  • super.myClassMethod() // runs in parent
  • myClassMethod() // overrides if method
  • // is in subclass

15
Review
  • A constructor is a method. T or F?
  • The constructors name is different from the
    class name. T or F?
  • A class can only have 1 constructor. T or F?
  • Constructors always receive arguments. T or F?

16
Review - 2
  • What is a superclass?
  • If all superclass constructors need to have
    arguments, what is necessary in the subclass
    constructors?
  • What key word is used in a subclass to run
    methods in a superclass?
  • Name one access modifier key word.

17
Review - 3
  • Which is more often declared as public methods
    or properties (variables)?
  • Why?
  • What does overriding mean?
Write a Comment
User Comments (0)
About PowerShow.com