Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.

Similar presentations


Presentation on theme: "Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses."— Presentation transcript:

1 Java Inheritance 1/13/2015

2 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.

3 Inheritance Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality.

4 “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.

5 Inheritance Hierarchy Vehicle Steering wheel Engine Size Color Car Type (Sedan, convertible wagon) Truck Tow rating Yugo Accord Ford F150 Toyota T100 Dodge Dakota Superclass Base class Generic class Subclass Inherits all of the members Extends the Superclass (Data and methods) from it’s superclass 5 Series Can access public and protected from the superclass

6 ‘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.

7 Other ‘Object’ methods clone finalize getClass hashCode notify, notifyAll, wait These are not in the AP level A exam subset.

8 Organizing into Inheritance Hierarchy Mountain Bike, Bicycle, Road bike, Tire Camel, height, Animal, weight, Cow, Hereford

9 What do a snake and a cow have in common? 1)They are both animals 2)They both have names 3)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!

10 Inheritance Hierarchy Animal Name Sound Snake Rattle? Cow Superclass Base class Generic class Subclass Inherits all of the members Extends the Superclass (Data and methods) from it’s superclass

11 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)

12 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.

13 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.

14 Inheritance What do you recall about inheritance? Review Quiz http://chortle.ccsu.edu/java5/Notes/chap50 /chap50quiz.htmlhttp://chortle.ccsu.edu/java5/Notes/chap50 /chap50quiz.html

15 Inheritance Hierarchy Animal Name Sound Snake Rattle? Cow Superclass Base class Generic class Subclass Inherits all of the members Extends the Superclass (Data and methods) from it’s superclass

16 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.

17 What do you recall about…? Class Parent Child Object Constructor Inheritance Public Protected “Bring out your notes…”

18 Now the ‘SUPERCLASS!” 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; } } Can you call getSound from a subclass of Animal? Can also write return this.getName() + “ says “ + this.getSound(); Can you change myName from a subclass of Animal?

19 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 } } 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). If you do not define a constructor in the subclass, it will call the superclass’s default constructor when the subclass object is created. What methods do Cows have?

20 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. You can also use, this.getSound() and this.getName(). Why can’t you just write myName for the name?

21 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); } }

22 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?

23 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.


Download ppt "Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses."

Similar presentations


Ads by Google