CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault...

Post on 27-May-2020

5 views 0 download

Transcript of CS 200 - Programming I: Inheritance · CS200-ProgrammingI:Inheritance MarcRenault...

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

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 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

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

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

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

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:WISCCOMPSCI200Spring2019

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