Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Inheritance.

Similar presentations


Presentation on theme: "Java Inheritance."— Presentation transcript:

1 Java Inheritance

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 Java uses the key word extends for inheriting from a class.
public class Location { public Location(int xCoord, int yCoord) } public class Color public Color(int redVal, int greenVal, int blueVal) public class Widget private Location myLoc; public Widget(int x, int y) myLoc = new Location(x, y); public class Thingy extends Widget private Color myColor; public Thingy(int x, int y, Color col) super(x, y); myColor = col; Example Java uses the key word extends for inheriting from a class. Color, Location, Widget, Thingy

5 “Is a…” vs. “has a…” “is a…” represents inheritance. A car “is a” vehicle. A Yugo “is a” car “Has a…” represents data/ instance variables/fields. A car “has a” steering wheel. A truck “has a” tow rating.

6 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 Dodge 5 Series Yugo Accord

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

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

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

10 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!

11 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

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

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

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

15 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

16 Time to Start a Farm Write a class that defines an Animal.
It will need at least the following Constructors Default and Given name and sound. State Fields for name and sound Behavior getName(), getSound(). toString()

17 Inheritance Hierarchy
Now for the Cow Animal Name Sound Snake Rattle? Cow

18 Inheritance Hierarchy
Now for the Snake Animal Name Sound Snake Rattle? Cow

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

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

21 Tracing Code in Classes
Constructors If there is a super() call, it must be the first line of the constructor. If there is no explicit super() call, it calls the parent’s default constructor If there is a super() call, it calls the corresponding constructor in the parent.

22 public class Location { public Location(int xCoord, int yCoord)
public class Location { public Location(int xCoord, int yCoord) } public class Color public Color(int redVal, int greenVal, int blueVal) public class Widget private Location myLoc; public Widget(int x, int y) myLoc = new Location(x, y); public class Thingy extends Widget private Color myColor; public Thingy(int x, int y, Color col) super(x, y); myColor = col; Assume that the following statement appears in a client program. Widget widg = new Thingy(100, 100, new Color(100, 100, 100)); What best describes the order on which the constructors will complete execution? Color, Location, Widget, Thingy

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

Similar presentations


Ads by Google