26
CS 200 - Programming I: Inheritance Marc Renault Department of Computer Sciences University of Wisconsin – Madison Spring 2019 TopHat Sec 3 (AM) Join Code: 560900 TopHat Sec 4 (PM) Join Code: 751425

CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

CS 200 - Programming I: Inheritance

Marc Renault

Department of Computer SciencesUniversity of Wisconsin – Madison

Spring 2019TopHat Sec 3 (AM) Join Code: 560900TopHat Sec 4 (PM) Join Code: 751425

Page 2: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

Inheritance

Inheritance

Page 3: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

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

Page 4: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

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

Page 5: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

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

Page 6: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

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

Page 7: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

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

Page 8: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

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

Page 9: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

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

Page 10: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

Inheritance

More on Access ModifiersAccess Modifiers

publicmembers 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

Page 11: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

Inheritance

More on Access ModifiersAccess Modifiers

publicmembers 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

Page 12: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

Inheritance

More on Access ModifiersAccess Modifiers

publicmembers 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

Page 13: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

Inheritance

More on Access ModifiersAccess Modifiers

publicmembers 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

Page 14: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

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

Page 15: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

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

Page 16: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

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

Page 17: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

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

Page 18: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

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

Page 19: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

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

Page 20: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

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

Page 21: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

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

Page 22: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

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

Page 23: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

Inheritance

Further Reading

COMP SCI 200: Programming IzyBooks.com, 2015.zyBook code:WISCCOMPSCI200Spring2019

Chapter 12. Creating ClassesChapter 13. More Classes

11/11

Page 24: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

Appendix References

Appendix

Page 25: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

Appendix References

References

Page 26: CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Spring2019 TopHatSec3(AM)JoinCode:560900

Appendix References

Image Sources I

https://brand.wisc.edu/web/logos/

http://www.zybooks.com/

12/11