Fundamentals of Software Development 1Slide 1 Inheritance Key ideas of Inheritance:Key ideas of Inheritance: –Overriding methods –Protected visibility –The “super” keyword These extend yesterday’s ideas about constructors and the “this” keyword
Fundamentals of Software Development 1Slide 2 Inheritance Basics (a checklist to refer to) Classes extending other classes:* Only have to describe how they are different from parentsOnly have to describe how they are different from parents Don’t have to repeat methods which are “good enough” in the parent (say, getShape)Don’t have to repeat methods which are “good enough” in the parent (say, getShape) Can replace or “extend” any method which isn’t “good enough” (say, getColor)Can replace or “extend” any method which isn’t “good enough” (say, getColor) Can share fields with a parent in a “protected” way (say, this.xloc)Can share fields with a parent in a “protected” way (say, this.xloc) Only need to declare imports they use (like java.awt.Color)Only need to declare imports they use (like java.awt.Color) Don’t have to redeclare interfaces of the parent (like Animate)Don’t have to redeclare interfaces of the parent (like Animate) Require the parent to have a void-signature constructor!Require the parent to have a void-signature constructor! * Other than those extending abstract classes like “Ball,” as you did with “Dud”
Fundamentals of Software Development 1Slide 3 Overriding Methods DudThatMoves will “extend” DudDudThatMoves will “extend” Dud If it defines a constructor with the same signature, that overrides Dud’s constructorIf it defines a constructor with the same signature, that overrides Dud’s constructor If it defines a method with the same signature, that overrides Dud’s methodIf it defines a method with the same signature, that overrides Dud’s method What do you think happens if our child class doesn’t override a method in the parent class?What do you think happens if our child class doesn’t override a method in the parent class? It’s exactly the same as in the parent class!
Fundamentals of Software Development 1Slide 4 Overriding Methods Examples: “Dud” probably did nothing in the method“Dud” probably did nothing in the method public void act() { In “DudThatMoves” you simply define anotherIn “DudThatMoves” you simply define another public void act() { And it will override the one in Dud, doing what you want.And it will override the one in Dud, doing what you want.
Fundamentals of Software Development 1Slide 5 Protected Visibility These you can share between the parent class and its children.These you can share between the parent class and its children. Suppose in Dud you defined:Suppose in Dud you defined: protected double xloc; protected double yloc; Then these definitions would be “shared” just with its children (not the same actual copy of the fields), for whatever they wanted to use them for.Then these definitions would be “shared” just with its children (not the same actual copy of the fields), for whatever they wanted to use them for.
Fundamentals of Software Development 1Slide 6 Protected Visibility An exception to the “not the same fields” rule is if a field is static, say:An exception to the “not the same fields” rule is if a field is static, say: protected static int numberOfDuds; Then this same field is shared by all Duds and all their children, like DudsThatMove. But no other classes can access them.Then this same field is shared by all Duds and all their children, like DudsThatMove. But no other classes can access them.
Fundamentals of Software Development 1Slide 7 The “super” Keyword It’s like the word “this,” only “super”:It’s like the word “this,” only “super”: In a child class, “super” refers to its parent.In a child class, “super” refers to its parent. So, you can say,So, you can say, super(some parameter) Under the constructor for a child class, to call the constructor for the parent class (and whatever it does will be done here)Under the constructor for a child class, to call the constructor for the parent class (and whatever it does will be done here) Or you can do call super.method() to run a method of the parent class.Or you can do call super.method() to run a method of the parent class.
Fundamentals of Software Development 1Slide 8 The “super” Keyword If you want to do what the parent class does for a constructor or a method, and more, then:If you want to do what the parent class does for a constructor or a method, and more, then: Probably you want to call the “super” first,Probably you want to call the “super” first, Then do the additional things you’d planned.Then do the additional things you’d planned. My inherited method 1. Call the super 2. Do whatever else is needed