cs 200 - programming i: inheritance
TRANSCRIPT
CS 200 - Programming I: Inheritance
Marc Renault
Department of Computer SciencesUniversity of Wisconsin – Madison
Fall 2018TopHat Sec 3 (AM) Join Code: 925964TopHat Sec 4 (PM) Join Code: 259495
Inheritance
Inheritance
Inheritance
Derived Classespublic class Goalie extends HockeyPlayer {
private int save;private int shots;
public Goalie(String name){super(name);
}
public void addShot (){shots ++;
}
public void addSave (){save ++;
}
public double getSavePct (){return (( double)save)/shots;
}
}
extends KeywordUsed to create a classbased on another class.A class can only extendone class.
SubclassThe class that extends another class.Also called: derived class, extended class, or child class.
SuperclassThe class that is extended.Also called: base class, or parent class.
1/11
Inheritance
Derived Classespublic class Goalie extends HockeyPlayer {
private int save;private int shots;
public Goalie(String name){super(name);
}
public void addShot (){shots ++;
}
public void addSave (){save ++;
}
public double getSavePct (){return (( double)save)/shots;
}
}
extends KeywordUsed to create a classbased on another class.A class can only extendone class.
SubclassThe class that extends another class.Also called: derived class, extended class, or child class.
SuperclassThe class that is extended.Also called: base class, or parent class.
1/11
Inheritance
Derived Classespublic class Goalie extends HockeyPlayer {
private int save;private int shots;
public Goalie(String name){super(name);
}
public void addShot (){shots ++;
}
public void addSave (){save ++;
}
public double getSavePct (){return (( double)save)/shots;
}
}
extends KeywordUsed to create a classbased on another class.A class can only extendone class.
SubclassThe class that extends another class.Also called: derived class, extended class, or child class.
SuperclassThe class that is extended.Also called: base class, or parent class.
1/11
Inheritance
Derived Classespublic class Goalie extends HockeyPlayer {
private int save;private int shots;
public Goalie(String name){super(name);
}
public void addShot (){shots ++;
}
public void addSave (){save ++;
}
public double getSavePct (){return (( double)save)/shots;
}
}
extends KeywordUsed to create a classbased on another class.A class can only extendone class.
SubclassThe class that extends another class.Also called: derived class, extended class, or child class.
SuperclassThe class that is extended.Also called: base class, or parent class.
1/11
Inheritance
What is super?
super KeywordFirst line in constructor to specify the superclassconstructor to use.Also can be used called superclass’ overridden methods.
Constructor Chaining1 If there is no explicit call to the superclass constructor in
subclass constructor, then super() is called implicitly.2 If super() doesn’t exist and no explicit call to the
superclass constructor, then compilation error.
2/11
Inheritance
TopHat Question 1What happens when this constructor is called?
public class Goalie extends HockeyPlayer {
private int save;private int shots;
public Goalie(String name){}
public void addShot (){shots ++;
}
public void addSave (){save ++;
}
public double getSavePct (){return (( double)save)/shots;
}
}
3/11
Inheritance
What is super?
super KeywordFirst line in constructor to specify the superclassconstructor to use.Also can be used called superclass’ overridden methods.
Constructor Chaining1 If there is no explicit call to the superclass constructor in
subclass constructor, then super() is called implicitly.2 If super() doesn’t exist and no explicit call to the
superclass constructor, then compilation error.
4/11
Inheritance
More on Access Modifiers
Access Modifierspublicmembers are accessible outside the class.privatemembers are only accessible inside the class.
default (no access modifier specified; also called packageaccess modifier) members are accessible to all classes in thepackage.protectedmembers have the same accessibility as thedefault plus accessible to subclasses.
PackagesClasses can be bundled into packages.Grouping of related types.First line in every file: package somepackage;
5/11
Inheritance
More on Access Modifiers
Access Modifierspublicmembers are accessible outside the class.privatemembers are only accessible inside the class.
default (no access modifier specified; also called packageaccess modifier) members are accessible to all classes in thepackage.protectedmembers have the same accessibility as thedefault plus accessible to subclasses.
PackagesClasses can be bundled into packages.Grouping of related types.First line in every file: package somepackage;
5/11
Inheritance
More on Access Modifiers
Access Modifierspublicmembers are accessible outside the class.privatemembers are only accessible inside the class.default (no access modifier specified; also called packageaccess modifier) members are accessible to all classes in thepackage.
protectedmembers have the same accessibility as thedefault plus accessible to subclasses.
PackagesClasses can be bundled into packages.Grouping of related types.First line in every file: package somepackage;
5/11
Inheritance
More on Access Modifiers
Access Modifierspublicmembers are accessible outside the class.privatemembers are only accessible inside the class.default (no access modifier specified; also called packageaccess modifier) members are accessible to all classes in thepackage.protectedmembers have the same accessibility as thedefault plus accessible to subclasses.
PackagesClasses can be bundled into packages.Grouping of related types.First line in every file: package somepackage;
5/11
Inheritance
Overriding Methods
OverridingSubclass method that overrides a superclass method must have:
the same name,the same parameters,the same return type (or subtype),have a no more restrictive access level.
Overriding vs overloadingKey difference: overriding has the same parameterswhereas over overloading has different parameters.
@Override annotation indicates to the compiler that amethod is suppose to be overriding a superclass method.
6/11
Inheritance
Overriding Methods
OverridingSubclass method that overrides a superclass method must have:
the same name,the same parameters,the same return type (or subtype),have a no more restrictive access level.
Overriding vs overloadingKey difference: overriding has the same parameterswhereas over overloading has different parameters.
@Override annotation indicates to the compiler that amethod is suppose to be overriding a superclass method.
6/11
Inheritance
Overriding Methods
OverridingSubclass method that overrides a superclass method must have:
the same name,the same parameters,the same return type (or subtype),have a no more restrictive access level.
Overriding vs overloadingKey difference: overriding has the same parameterswhereas over overloading has different parameters.@Override annotation indicates to the compiler that amethod is suppose to be overriding a superclass method.
6/11
Inheritance
Polymorphism
TopHat Question 2What is the output?ArrayList <Object > arrL = new ArrayList <Object >();arrL.add("foo");arrL.add (1); arrL.add (2.5);System.out.print(arrL);
Virtual method invocationThe JVM determines the method to call based on the actualobject and not the reference type variable.
7/11
Inheritance
Polymorphism
TopHat Question 2What is the output?ArrayList <Object > arrL = new ArrayList <Object >();arrL.add("foo");arrL.add (1); arrL.add (2.5);System.out.print(arrL);
Virtual method invocationThe JVM determines the method to call based on the actualobject and not the reference type variable.
7/11
Inheritance
TopHat Question 3What is the output?public class UseAnimal {
public static void main(String [] main) {Animal a = new Fish ();System.out.print(a.speak ());
}
}
class Animal {
public String speak () {return "---";
}
}
class Fish extends Animal {
public String speak () {return "GLUB!";
}
}
class Dog extends Animal {
public String speak () {return "WOOF!";
}
}
class Cat extends Animal {
public String speak () {return "MEOW!";
}
}
class Cow extends Animal {
public String speak () {return "MOO!";
}
}
8/11
Inheritance
Is-a Relationship
Object
HockeyPlayer
name: String
Goalie
instanceof OperatorsomeObj instanceof someClass
true if someObj is the same class or asubclass of someClass.
Is-a vs Has-aIs-a: inheritance – Goalie is aHockeyPlayer
Has-a: contains – HockeyPlayer has aString
9/11
Inheritance
Is-a Relationship
Object
HockeyPlayer
name: String
Goalie
instanceof OperatorsomeObj instanceof someClass
true if someObj is the same class or asubclass of someClass.
Is-a vs Has-aIs-a: inheritance – Goalie is aHockeyPlayer
Has-a: contains – HockeyPlayer has aString
9/11
Inheritance
TopHat Question 4
What is the output?
Object g = new Goalie("Brodeur");System.out.print(g instanceof Goalie );System.out.print(" ");System.out.print(g instanceof HockeyPlayer );System.out.print(" ");System.out.print(g instanceof Object );System.out.print(" ");System.out.print(null instanceof Goalie );
10/11
Inheritance
Further Reading
COMP SCI 200: Programming IzyBooks.com, 2015.zyBook code:WISCCOMPSCI200Fall2018
Chapter 12. Creating ClassesChapter 13. More Classes
11/11
Appendix References
Appendix
Appendix References
References
Appendix References
Image Sources I
https://brand.wisc.edu/web/logos/
http://www.zybooks.com/
12/11