Download presentation
Presentation is loading. Please wait.
Published byGiles Joseph Modified over 9 years ago
1
Superclasses and Subclasses in Java Computer Science 3 Gerb Objective: Understand superclass methods and the “this” keyword in Java
2
Recall that When class2 extends class1, we say… –Class2 is a subclass of class1 –Class1 is a superclass of class2 When class2 defines a method defined in class1, we say that method is specialized. –If myMethod is specialized in class2, then anytime it is invoked on an instance of class2, class2’s version will be called, not class1’s –This could be a problem.
3
A dilemma Suppose we have a method myMethod in class1, which is extended by class2 –We want to specialize myMethod in class2 –But we have shut ourselves off from the myMethod in class1. –If we want to use class1’s myMethod to help us implement class2’s myMethod. We’re out of luck! Woe is us…
4
Super to the rescue Java provides a keyword called super Super allows us to access a method in a superclass that we specialized. E.g. super.myMethod() means “call the myMethod defined in the superclass before I specialized it.”
5
Example mySuper defines a method myMeth that calls the setUp.setMeUpWell function. (Note that this is a java shorthand for the object on which the method is called.) class mySuper { public void myMeth(){ setUp.setMeUpWell(this);}} mySub specializes myMeth, to call the method called setUp.setMeUpBetter, but now can’t call mySuper’s myMeth method. class mySub extends mySuper { myMeth(){ setUp.setMeUpBetter();}}
6
Example Cont’d class mySuper { public void myMethod(){ setUp.setMeUpWell(this);}} Can use super to call mySuper’s myMeth method: class mySub extends mySuper { public void myMethod(){ super.myMethod(); setUp.setMeUpBetter();}}
7
A note on the this keyword The this keyword can always be used in an instance method to refer to the instance upon which the method was called. E.g.: –Give the following definition public void aMethod() { anotherClass.anotherMethod(this);} –The following method invocation anInstance.aMethod() –Would result in anotherClass.anotherMethod being called with anInstance as a parameter.
8
Another way to use super Super is frequently used to call a constructor for a superclass When used in this way –Must be the first line in the subclass’s constructor –Is used by itself without a period followed by a method name
9
For Example class class1 { class1(int i){ … //Statements to construct class1 } class class2 extends class1 { class2(){ super(5);//Constructs according to //class1’s constructor … //Additional statements to } //Construct class2. }
10
Why super is needed When an instance of a subclass is created, the default constructor of the superclass is always called. Compiler error if superclass has no default constructor. Super allows you to decide which superclass constructor to call, and with which parameters.
11
Summary Use the super keyword to call methods in a superclass that have been overridden. Use the this keyword to refer to the object on which a method was invoked. When super is used to invoke a constructor –Must be the first statement in the subclass constructor. –Must not use a period or a method name.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.