1 / 19

ECE 122

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 {

catrin
Download Presentation

ECE 122

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ECE 122 March 24

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

  3. 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(){} }

  4. 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(){} }

  5. Do you notice anything?

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

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

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

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

  10. Add a super class, Animal • It contains all the common instance variables • It contains all the common methods with a default implementation.

  11. 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(){} }

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

  13. What is the syntax for inheritance? public class Cat extends Animal { }

  14. Here is my Simplified Cat class public class Cat extends Animal { public void makeNoise() { //Cat's meow } public void eat() { //Eat meat } }

  15. Sheep, dog, tiger, ….??? • I will do the same thing! • I am going to vacation in one week!

  16. Here is my simplified Sheep class public class Sheep extends Animal { public void makeNoise() { //Sheep's meeeeier ? } public void eat() { //Eat grass } }

  17. 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!

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

  19. Assignments • Read “Java 2”, Chapter 7 • Read “Head First Java”, Chapter 7

More Related