Inheritance part 2 Subclassing COMP 401 Spring 2013 Lecture 9 2 6 2013 MoBvaBng Example lec9 v1 Suppose we re wriBng a university management system Interfaces Person get rst and last name get set address Student add credits get set status i e freshman sophomore junior senior Professor promote get rank i e assistant associate full Classes StudentImpl implements Person Student ProfessorImpl implements Person Professor lec9 v1 Notes Student and Professor interfaces should be subinterfaces of Person Presumably implemenBng Student or Professor implies also being a Person lec9 v2 Notes CasBng no longer necessary Anything that is a Student is also a Person Anything that is a Professor is also a Person NoBce how StudentImpl and ProfessorImpl implement the Person part of Student and Professor EssenBally the same implementaBon Private elds for rst last and address Same de niBons for Person methods When two or more classes implement the same interface in the same way then subclassing can help Extending Classes Declared with extends keyword Original class Parent parent class superclass New class Child child class subclass extended class Subclasses inherit elds and methods from the parent class Collect the common implementaBon details from related classes into a single parent class De ne each related class as a subclass that just adds the details that are not in common lec9 v3 Notes NoBce parallel with interface structure PersonImpl implements Person StudentImpl extends PersonImpl and implements Student which extends Person ProfessorImpl extends PersonImpl and implements Professor which extends Person Subclass constructor calls superclass constructor using super Must be rst line of subclass constructor Can sBll chain to a di erent constructor Subinterface vs Subclass Extending interface only added behavior to contract Since interfaces don t specify and don t care how contract is ful lled Extending class creates a new class that shares internal implementaBon details of its super class COMP 401 Spring 2012 7 public class Bicycle the Bicycle class has three elds public int cadence public int gear public int speed the Bicycle class has one constructor public Bicycle int startCadence int startSpeed int startGear gear startGear cadence startCadence speed startSpeed the Bicycle class has four methods public void setCadence int newValue cadence newValue public void setGear int newValue gear newValue public void applyBrake int decrement speed decrement public void speedUp int increment speed increment public class MountainBike extends Bicycle the MountainBike subclass adds one eld public int seatHeight the MountainBike subclass has one constructor public MountainBike int startHeight int startCadence int startSpeed int startGear cadence startCadence speed startSpeed gear startGear seatHeight startHeight the MountainBike subclass adds one method public void setHeight int newValue seatHeight newValue MountainBike m new MountainBike 10 15 0 1 m setCadence 20 m setGear 2 m applyBreak 5 m speedUp 8 m setHeight 12 hgp docs oracle com javase tutorial java IandI subclasses html public class MountainBike extends Bicycle Extending a class is like wriBng a new class that has all the same details of the original class plus adding addiBonal stu speci c to subclass public class MountainBike public int cadence public int gear public int speed public int seatHeight public void setCadence int newValue cadence newValue public void setGear int newValue gear newValue public void applyBrake int decrement speed decrement public void speedUp int increment speed increment public void setHeight int newValue seatHeight newValue COMP 401 Spring 2012 9 Is A RelaBonships for Subclasses Like interfaces is a relaBonship is transiBve up the subclass hierarchy A MountainBike object is a Bicycle Because you inherit everything your parent provides by de niBon you also implement any interfaces your parent implements And so on all the way up the hierarchy COMP 401 Spring 2012 10 Is A F or S ubclasses Objects of type A implement class A implements InterA class B extends A implements InterB class C extends B implements InterC interface InterA A is a Inter A Objects of type B implement interface InterB and InterA B is a A B is a InterA B is a InterB Objects of type C implement interface InterC InterB and InterA C is a A C is a B C is a InterA C is a InterB C is a InterC COMP 401 Spring 2012 11 Object All classes inherit from Object Top of the class hierarchy Since every class must inherit from Object don t actually need to specify it So when we say this public class MyClass We were implicitly doing this public class MyClass extends Object COMP 401 Spring 2012 12 Object cont d Because all classes implicitly have Object as a superclass ancestor A variable with data type Object can hold anything But then restricted to just the methods that are de ned at the level of Object Public methods that all objects have public boolean equals Object o public String toString COMP 401 Spring 2012 13 Instance Fields Subclass has direct access to public and protected instance elds Public Everyone has access Generally not a good idea Breaks encapsulaBon Private Only class has access Generally recommended as default Subclasses however also shut out Protected Class and subclasses have access Like private i e appropriate use of encapsulaBon but allows for subclassing even if outside of package lec9 v4 COMP 401 Spring 2012 14 Access Modi er Chart Class Package Subclass World public YES YES YES YES protected YES YES YES NO no modi er YES YES NO NO private YES NO NO NO
View Full Document
Unlocking...