Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 14 - Monday CS 121.

Similar presentations


Presentation on theme: "Week 14 - Monday CS 121."— Presentation transcript:

1 Week 14 - Monday CS 121

2 Last time What did we talk about last time? Image manipulation Lab 13

3 Questions?

4 Project 5

5 Concept of Inheritance

6 Inheritance The idea of inheritance is to take one class and generate a child class This child class has everything that the parent class has (members and methods) But, you can also add more functionality to the child The child can be considered to be a specialized version of the parent

7 Code reuse The key idea behind inheritance is safe code reuse
You can use old code that was designed to, say, sort lists of Vehicles, and apply that code to lists of Cars All that you have to do is make sure that Car is a subclass (or child class) of Vehicle

8 Subclass relationship
Java respects the subclass relationship If you have a Vehicle reference, you can store a Car object in that reference A subclass (in this case a Car) is a more specific version of the superclass (Vehicle) For this reason, you can use a Car anywhere you can use a Vehicle You cannot use a Vehicle anywhere you would use a Car

9 Subclass example As long as Car is a subclass of Vehicle, we can store a Car in a Vehicle reference Even in an array is fine Storing a Vehicle into a Car doesn’t work Vehicle v = new Car("Lancer Evolution"); // perfectly okay Vehicle[] vehicles = new Vehicle[100]; for( int i = 0; i < vehicles.length; i++ ) vehicles[i] = new RocketShip(); // cool Car c = new Vehicle(); // gives error

10 Inheritance Mechanics

11 Creating a subclass All this is well and good, but how do you actually create a subclass? Let’s start by writing the Vehicle class public class Vehicle { public void travel(String destination) { System.out.println("Traveling to " + destination); }

12 Extending a superclass
We use the extends keyword to create a subclass from a superclass A Car can do everything that a Vehicle can, plus more public class Car extends Vehicle { private String model; public Car(String s) { model = s; } public String getModel() { return model; } public void startEngine() { System.out.println("Vrooooom!"); }

13 Power of inheritance There is a part of the Car class that knows all the Vehicle members and methods Car car = new Car("Camry"); System.out.println( car.getModel() ); //prints "Camry" car.startEngine(); //prints "Vrooooom!" car.travel( "New York City" ); //prints "Traveling to New York City"

14 A look at a Car Each Car object actually has a Vehicle object buried inside of it If code tries to call a method that isn’t found in the Car class, it will look deeper and see if it is in the Vehicle class The outermost method will always be called Car model getModel() startEngine() Vehicle travel()

15 Overriding Methods

16 Adding to existing classes is nice…
Sometimes you want to do more than add You want to change a method to do something different You can write a method in a child class that has the same name as a method in a parent class The child version of the method will always get called This is called overriding a method

17 Mammal example We can define the Mammal class as follows:
public class Mammal { public void makeNoise() { System.out.println("Grunt!"); }

18 Mammal subclasses From there, we can define the Dog, Cat, and Human subclasses, overriding the makeNoise() method appropriately public class Dog extends Mammal { public void makeNoise() { System.out.println("Woof"); } } public class Cat extends Mammal { public void makeNoise() { System.out.println("Meow"); } } public class Human extends Mammal { public void makeNoise() { System.out.println("Hello"); } }

19 Inheritance Examples

20 Hunters and prey simplified
Everyone seemed to like the hunters and prey example Let’s use inheritance to make it easier to program Hunters and prey are very similar, except that hunters move toward prey and prey move away With inheritance we can reuse code We only need to change the behavior portion

21 Prey class The Prey class has:
x Location: double y Location: double Aliveness: boolean Speed: double Color: StdDraw.Color Size: double We give the Prey class distance() methods and draw() methods

22 Hunter class The Hunter class now extends the Prey class, getting everything that it had before We call the Prey constructor with different size, color, and speed parameters Then we just need to change the update() method to search for Prey instead of running from Hunters

23 getClass() method To make the Hunter and Prey classes work, we now need to know if a specific object belongs to a given type Every object has a getClass() method We can test to see if an object’s getClass() method gives back Hunter.class or Prey.class By doing so, we can tell the class of a given object

24 Further expansion of hunters and prey
What if we create a super hunter? Super hunters are larger and faster than hunters, and try to eat everything, including each other We make SuperHunter extend Hunter We call the Hunter constructor with the right speed, size, and color Then, we change the update() method to reflect the right behavior

25 Quiz

26 Upcoming

27 Next time… File I/O

28 Reminders Keep working on Project 5


Download ppt "Week 14 - Monday CS 121."

Similar presentations


Ads by Google