Java Inheritance
Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses Be able to use the keyword extends to create a class that inherits attributes and behaviors from another class. Understand how constructors are used in inheritance hierarchies. Understand the class Object, the direct or indirect superclass of all classes in Java.
Inheritance Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality.
“Is a…” vs. “has a…” “is a…” represents inheritance. A car “is a” vehicle. A Yugo “is a” car “Has a…” represents data. A car “has a” steering wheel. A truck “has a” tow rating.
Inheritance Hierarchy Subclass Inherits all of the members Extends the Superclass (Data and methods) from it’s superclass Vehicle Steering wheel Engine Size Color Superclass Base class Generic class Can access public and protected from the superclass Car Type (Sedan, convertible wagon) Truck Tow rating Ford F150 Toyota T100 Dodge Dakota 5 Series Yugo Accord
‘Object’ class (Super duper) All classes in Java inherit directly or indirectly from the Object class. So every class has the following methods .equals: Compares two objects for equality and returns true if they are equal and false otherwise. object1.equals(object2)(If this method is not redefined in the subclass, then it will compare addresses of the objects, just like ==) .toString(): Returns a string representation of an object.
Other ‘Object’ methods clone finalize getClass hashCode notify, notifyAll, wait These are not in the AP level A exam subset.
Organizing into Inheritance Hierarchy Mountain Bike, Bicycle, Road bike, Tire Camel, height, Animal, weight, Cow, Hereford
What do a snake and a cow have in common? They are both animals They both have names They both make sounds But… A snake can have a rattle… For the discussion we will create an ‘Animal’ class out of what is common between a cow and a snake. Then use these in creating classes for the cow and snake. Using inheritance!
Inheritance Hierarchy Subclass Inherits all of the members Extends the Superclass (Data and methods) from it’s superclass Superclass Base class Generic class Animal Name Sound Snake Rattle? Cow
Some Super Rules A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass using a super() call. (Unless no constructor is defined, then the parent’s default constructor is called)
What children (subclasses) can do. The inherited public and protected fields (variables) can be used directly, just like any other fields. You can declare a field (variable) in the subclass with the same name as the one in the superclass, thus hiding it (not recommended). You can declare new fields in the subclass that are not in the superclass. The public and protected inherited methods can be used directly as they are. You can write a new instance (non static) method in the subclass that has the same signature as the one in the superclass, thus overriding it. You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it. You can declare new methods in the subclass that are not in the superclass. You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super. You can access public and protected fields and methods of the super class.
What children can’t do Access private fields or methods of a super class. If parents lock the safe, children can’t get into it without permission (a public accessor method) Limit the access (make a public method into a private method) when overriding a method. If parents say anyone can have cookies, then children cannot lock up the cookie jar.
Inheritance Hierarchy Subclass Inherits all of the members Extends the Superclass (Data and methods) from it’s superclass Superclass Base class Generic class Animal Name Sound Snake Rattle? Cow
Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses Be able to use the keyword extends to create a class that inherits attributes and behaviors from another class. Understand how constructors are used in inheritance hierarchies. Understand the class Object, the direct or indirect superclass of all classes in Java.
What do you recall about…? Class Parent Child Object Constructor Inheritance Public Protected “Bring out your notes…”
Now the ‘SUPERCLASS!” Can you change myName from a subclass of Animal? public class Animal { private String myName; //Subclasses must use getName to see this private String mySound; public Animal () //Default Constructor myName = ""; mySound = ""; } public Animal (String newName, String newSound) //Constructor that takes a name and sound myName = newName; mySound = newSound; public String getName() //Accessor Methods return myName; public String getSound() return mySound; public String toString() //toString returns the animals information return myName + " says " + mySound; Now the ‘SUPERCLASS!” Can you change myName from a subclass of Animal? Can you call getSound from a subclass of Animal? Can also write return this.getName() + “ says “ + this.getSound();
What methods do Cows have? Now the Cow public class Cow extends Animal { //All of the instance variables are inherited from the Animal class //Constructor public Cow (String newName, String newSound) //It will use Animal’s constructor to create the cow super(newName, newSound); //Calls the superclass's constructor } What methods do Cows have? Constructing Cows The constructor will call the superclasses constructor either with the super method (must be the first line of the constructor) or it will call the superclasses default constructor (implicitly, that is you don’t write the super call). If you do not define a constructor in the subclass, it will call the superclass’s default constructor when the subclass object is created.
Snake public class Snake extends Animal { private boolean myRattle; // //Constructor public Snake (String newName, String newSound, boolean rattle) super(newName, newSound); myRattle = rattle; } //Will need to overwrite toString so it will display stripe information public String toString() String message =("The snake " + getName() + " says "); message += getSound() + " and rattle = " + myRattle; return message; Make the call to the super first in the constructor. Why can’t you just write myName for the name? You can also use, this.getSound() and this.getName().
Driving this zoo public class AnimalDriver { public static void main(String args[]) Cow betsy = new Cow("Betsy", "moo"); System.out.println(betsy); Snake terry = new Snake("Terry", "hiss", TRUE); System.out.println(terry); }
Review What can subclasses do? What is ‘this’? Can a child redefine a parent’s methods? Can a subclass access all of the superclass methods What methods are in all classes? How do you call the parent class constructor from one of its children? How many parents can a child have? How many children can a parent have? What is a class variable? What is an instance variable?
Your Assignment: Animals Add two more classes of animals Enhance each class with at least one more instance variable. At least one of these classes must extend another class you created. Create a driver class (application, public static void main…) that will demonstrate that your classes are working. Turn in each of your classes and the driver class.