Title: Extending Classes
1Extending Classes
- Inheritance and Polymorphism
2What is Inheritance?
- Represents the is a relationship
- Super Class parent, very generic
- Derived Class superset of Super class
- all of the components of the Super Class PLUS
components specific to the new Class - You Can Inherit in a Hierarchy
-
Animal
is a
is a
Dog
Cat
is a
Boxer
3 Super Class Animal
- public class Animal
-
- protected int age
- public Animal( ) //default Constructor
- age 0
- public Animal( int a ) //overloaded
Constructor - age a
- public void setAge( int years )
- age years
- public int getAge( )
- return age
- public void speak( )
- System.out.println(I do not know what
noise to make) -
4Dog - Derived Class from Animal
- public class Dog extends Animal
-
- private int numberOfFleas
//attribute specific to Dog -
- public Dog( )
- super( )
-
- public Dog( int inAge )
- super( inAge )
-
- public void setNumberOfFleas( int myFleas )
- numberOfFleas myFleas
- public int getNumberOfFleas( )
- return numberOfFleas
-
- public void speak( )
//overridden
5Cat - Derived Class from Animal
- public class Cat extends Animal
-
- private int numberOfLives
//attribute specific to Cat -
- public Cat( )
- super( )
-
- public Cat( int inAge )
- super( inAge )
-
- public void setNumberOfLives( int myLives )
- numberOfLives myLives
- public int getNumberOfLives( )
- return numberOfLives
-
- public void speak( )
//overridden
6What is Polymorphism?
- Changing the implementation of Inherited Methods
to be more specific for a Derived Class. - Allows objects of different classes related by
inheritance to respond differently to the same
message. - Example
- Using the same message speak( ) Make a dog
bark and a cat meow. - Accomplished by overriding the implementation
of a method.
7Method Overriding
- Super Class
- Provides method interface
- Has simple ( or no ) method definition
- Derived Class
- uses exact same method interface, and rewrites
the method definition to accommodate the specific
needs of the derived class objects.
8Using Polymorphism
- public class AnimalDriver
-
- public static void main( String args )
-
- Animal animalRef
//declare a generic reference to an Animal - Dog fido new Dog( 5 ) //create a
Dog object with 5 fleas - Cat boots new Cat( 10 ) //create a
Cat object with 10 lives - animalRef fido //point
the animalRef reference to the Dog - //run the Dog version of speak( )
- System.out.println(Fido says
animalRef.speak( ) ) - animalRef boots
//point the animalRef reference to the Cat - //run the Cat version of speak( )
- System.out.println(Boots says
animalRef.speak( ) ) -
-
9Abstract Methods
- No method definition (code) in super class
- The base class only provides the interface
(function signature). - Code for the method is written in each derived
class - Subclass implementation meets specific subclass
needs. - Using a reference to the super class, you can
treat all objects the same - because the super class DOES provide the
interface for the method. - A class that has at least one abstract method is
considered to be an abstract class.
10Abstract Class Animal
- public abstract class Animal
-
- protected int age
- public Animal( ) //default Constructor
-
- public Animal( int inYears ) //overloaded
Constructor - setAge( inYears )
- public void setAge( int years )
- age years
- public int getAge( )
- return age
-
//abstract method only has signature - public abstract void speak( ) //in super
class
11- Concrete Classes
- Classes from which objects can be instantiated
- Abstract Classes
- Contains at least one abstract method
- No objects can be instantiated from abstract
classes
12Javas Universal Superclass
- Every Class is a subclass of Javas Object
class. - The Object class provides basic implementation
for useful methods. - These methods are available in every class in
Java, regardless of whether you created it or
not. They are inherited from the Object class.
- toString( ) returns a String representation of
an object. - equals( ) provides comparison logic.
- These inherited methods should be over-ridden so
that they behave appropriately for your class.
13Partial Java Class Hierarchy
Object Parent Class for All Java
Classes toString() writes the hashcode and
Class Name to the screen
by default.
Super Classes You Create toString() You
override to print contents of
data members - or other
information of your choice.
Java Classes toString() - Inherit the parent
class implementation
Sub Class You can override Here.
Sub Class You can override Here, too.
Sub Class
Sub Class
14- public abstract class Animal
-
- protected int yearsOld
- protected int animalYears
- public Animal( ) //default Constructor
- setYearsOld( 0 )
- public Animal( int inYears ) //overloaded
Constructor - setYearsOld( inYears )
- public void setYearsOld( int years )
- yearsOld years
- .
- .
- .
-
//abstract method only has signature - public abstract void speak( ) //in super
class
15Lab
- Write Cat and Dog classes that extend Animal.
16Project 2
- Define an abstract base class Shape that includes
2 protected data members of your Point type and a
public abstract method show( ) to output a the x
y values for a shape. - Derive subclasses for Lines and Rectangles. You
can represent a Line as two points. You can
represent a Rectangle as two points on diagonally
opposite corners. Implement the toString( )
method for each class. - Write a driver program to test your classes.
Instantiate one of each type (Line and
Rectangle), use the show( ) method to display
each objects Point values. - Turn in a hard copy of all classes Shape,
Point, Line, Rectangle. A copy of your output,
and your disk