Unformatted text preview:

What is Inheritance?Masai ClassKikuyu ClassProblem: Code DuplicationSolution: InheritanceKenyan SuperclassMasai SubclassKikuyu SubclassInheritance Quiz 1Inheritance RulesSubclass ConstructorImplicit Super Constructor CallInheritance Quiz 2Overriding MethodsCalling Superclass MethodsWhich Lines Don't Compile?Remember Casting?Which Castings Will Fail?Programming ExampleObject ClassLecture 12InheritanceMIT-AITI Kenya 2005June 28th, 2005©2005MIT-Africa Internet Technology InitiativeWhat is Inheritance? In the real world: We inherit traits from our mother and father. We also inherit traits from our grandmother, grandfather, and ancestors. We might have similar eyes, the same smile, a different height . . . but we are in many ways "derived" from our parents.  In software: Object inheritance is more well defined! Objects that are derived from other object "resemble" their parents by inheritingboth state (fields) and behavior (methods).©2005MIT-Africa Internet Technology InitiativeMasai Classpublic class Masai {private String name;private int cows;public Masai(String n, int c) {name = n;cows = c;}public String getName() { return name; }public int getCows() { return cows; }public void speak() {System.out.println(“Masai");}}©2005MIT-Africa Internet Technology InitiativeKikuyu Classpublic class Kikuyu {private String name;private int money;public Kikuyu(String n, int m) {name = n;money = m;}public String getName() { return name; }public int getMoney() { return money; }public void speak() {System.out.println(“Kikuyu");}}©2005MIT-Africa Internet Technology InitiativeProblem: Code Duplication• Dog and Cat have the name field and the getName method in common• Classes often have a lot of state and behavior in common• Result: lots of duplicate code!©2005MIT-Africa Internet Technology InitiativeSolution: Inheritance• Inheritance allows you to write new classes that inherit from existing classes• The existing class whose properties are inherited is called the "parent" or superclass• The new class that inherits from the super class is called the "child" or subclass• Result: Lots of code reuse!©2005MIT-Africa Internet Technology InitiativeMasaiString nameint cowsString getName()int getCows()void speak()KikuyuString nameint moneyString getName()int getMoney()void speak()usinginheritanceMasaiint cowsint getCows()void speak()Kikuyuint moneyint getMoney()void speak()KenyanString nameString getName()superclasssubclasssubclass©2005MIT-Africa Internet Technology InitiativeKenyan Superclasspublic class Kenyan {private String name;public Kenyan(String n) {name = n;}public String getName() {return name;}}©2005MIT-Africa Internet Technology InitiativeMasai Subclasspublic class Masai extends Kenyan {private int cows;public Masai(String n, int c) {super(n); // calls Kenyan constructorcows = c;}public int getCows() {return cows;}public void speak() {System.out.println(“Masai");}}©2005MIT-Africa Internet Technology InitiativeKikuyu Subclasspublic class Kikuyu extends Kenyan {private int money;public Kikuyu(String n, int m) {super(n); // calls Kenyan constructormoney = m;}public int getMoney() {return money;}public void speak() {System.out.println(“Kikuyu");}}©2005MIT-Africa Internet Technology InitiativeInheritance Quiz 1• What is the output of the following?Masai d = new Masai(“Johnson" 23);Kikuyu c = new Kikuyu(“Sheila", 2200);System.out.println(d.getName() + " has " +d.getCows() + " cows");System.out.println(c.getName() + " has " +c.getMoney() + " shillings");Johnson has 23 cowsSheila has 2200 shillings(Masai and Kikuyu inherit the getName method from the Kenyan super class)©2005MIT-Africa Internet Technology InitiativeInheritance Rules• Use the extends keyword to indicate that one class inherits from another• The subclass inherits all the fields and methods of the superclass• Use the super keyword in the subclass constructor to call the superclassconstructor©2005MIT-Africa Internet Technology InitiativeSubclass Constructor• The first thing a subclass constructor must do is call the superclass constructor• This ensures that the superclass part of the object is constructed before the subclass part• If you do not call the superclass constructor with the super keyword, and the superclasshas a constructor with no arguments, then that superclass constructor will be called implicitly.©2005MIT-Africa Internet Technology InitiativeImplicit Super Constructor Callthen this Beef subclass:public class Beef extends Food {private double weight;public Beef(double w) {weight = w}}is equivalent to:public class Beef extends Food {private double weight;public Beef(double w) {super();weight = w}}If I have this Food class:public class Food {private boolean raw;public Food() {raw = true;}}©2005MIT-Africa Internet Technology InitiativeInheritance Quiz 2public class A {public A() { System.out.println("I'm A"); }}public class B extends A {public B() { System.out.println("I'm B"); }}public class C extends B {public C() { System.out.println("I'm C"); }}What does this print out?C x = new C();I'm AI'm BI'm C©2005MIT-Africa Internet Technology Initiative• Subclasses can override methods in their superclass• What is the output of the following?ThermUS thermometer = new ThermUS(100);System.out.println(thermometer.getTemp());class ThermUS extends Therm {public ThermUS(double c) {super(c);}// degrees in Fahrenheitpublic double getTemp() {return celsius * 1.8 + 32;}}class Therm {public double celsius;public Therm(double c) {celsius = c;}public double getTemp() {return celcius;}}212Overriding Methods©2005MIT-Africa Internet Technology InitiativeCalling Superclass Methods• When you override a method, you can call the superclass's copy of the method by using the syntax super.method()class Therm {private double celsius;public Therm(double c) {celcius = c;}public double getTemp() {return celcius;}}class ThermUS extends Therm {public ThermUS(double c) {super(c);}public double getTemp() {return super.getTemp()* 1.8 + 32;}}©2005MIT-Africa Internet Technology InitiativeWhich Lines Don't Compile?public static void main(String[] args) {Kenyan a1 = new Kenyan();a1.getName();a1.getCows();a1.getMoney();a1.speak();Kenyan a2 = new Masai();a2.getName();a2.getCows();a2.getMoney();a2.speak();Masai d = new Masai();d.getName();d.getCows();d.getMoney();d.speak();}// Kenyan does not have getCows// Kenyan does not have getMoney// Kenyan does not have speak// Kenyan does not have getCows// Kenyan does


View Full Document

MIT SP 772 - Inheritance

Download Inheritance
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Inheritance and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Inheritance 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?