190 likes | 373 Views
ECE 122. March 24. Motivation. I am tasked to write two classes, one for cat, one for sheep. That is easy. I roll up my sleeve and get it done!. Here is my class for cat. import java.io.*;//importing java I/O package public class Cat1 {
E N D
ECE 122 March 24
Motivation • I am tasked to write two classes, one for cat, one for sheep. • That is easy. I roll up my sleeve and get it done!
Here is my class for cat import java.io.*;//importing java I/O package public class Cat1 { File imageFile; //The filename storing the image of the cat String food; //"meat" int hunger;//hunger level of the cat. It changes depending on when and how much the cat //eats int locationX;//X coordinate of the cat in space int locationY; //Y coordinate of the cat in space public void makeNoise(){} public void eat(){} public void sleep(){} public void roam(){} }
Here is my class for sheep import java.io.*;//importing java I/O package public class Sheep1 { File imageFile; //The filename storing the image of the sheep String food; //“grass" int hunger;//hunger level of the sheep. It changes depending on when and how much the //sheep eats int locationX;//X coordinate of the sheep in space int locationY; //Y coordinate of the sheep in space public void makeNoise(){} public void eat(){} public void sleep(){} public void roam(){} }
Observations • Sheep1 class and Cat1 class are very much alike. • They have same instance variables • They have same methods • Can we save some typing and maintenance efforts by sharing? • My supervisor asks me to write more classes for lion, hippo, tiger, cow, wolf, dog,… • Where is my vacation???
Inheritance • Put common codes in a super class. • Subclass inherits from super class, and REUSE codes from super class. • An inheritance relationship means subclass inherits the members (both instance variables and methods) of super class. • Subclass can add new methods and variables of its own. • Subclass can override the methods it inherits from super class. • Instance variables are not overriden.
IS-A Relationship • Inheritance is not just for reusing code. • A subclass should be a specific kind of super class. E.g. A cat IS A specific kind of Animal.
What am I going to do? • I need a vacation. • I need to finish my work before going to vacation. • I want to redesign my objects. • I have identified IS-A relationship. Cat is a specific kind of Animal. So do Sheep, … • I will use inheritance that I just learned.
Add a super class, Animal • It contains all the common instance variables • It contains all the common methods with a default implementation.
Here is my super class, Animal import java.io.*;//importing java I/O package public class Animal { File imageFile; //The filename storing the image of the animal String food; //"meat" or "grass" int hunger;//hunger level of the animal. It changes depending on //when and how much the animal eats int locationX;//X coordinate of the animal in space int locationY; //Y coordinate of the animal in space public void makeNoise(){} public void eat(){} public void sleep(){} public void roam(){} }
Simplify my Cat1 class • Rename my cat class from Cat1 to Cat • Cat class inherits all instance variables from Animal class, so I will not write it. • Cat class inherits all methods from Animal class, so I overwrite methods that should be implemented differently. • Cat makes noise differently, so I overwrite makeNoise() method. • Cat eats meat, not grass, so I need to overwrite eat() method to specify this.
What is the syntax for inheritance? public class Cat extends Animal { }
Here is my Simplified Cat class public class Cat extends Animal { public void makeNoise() { //Cat's meow } public void eat() { //Eat meat } }
Sheep, dog, tiger, ….??? • I will do the same thing! • I am going to vacation in one week!
Here is my simplified Sheep class public class Sheep extends Animal { public void makeNoise() { //Sheep's meeeeier ? } public void eat() { //Eat grass } }
Which method is called actually? • Both Animal (super class) and Cat (subclass) have makeNoise() method. After I instantiate a Cat object, I call makeNoise() method. Which method is actually called? Super class? Or subclass? • Let’s test it out!
Which method is called, actually? • The method of the most specific class/object. • If a method is not overriden in subclass, the super class method is called. • If a method is overriden in subclass, the subclass’s method is called.
Assignments • Read “Java 2”, Chapter 7 • Read “Head First Java”, Chapter 7