Professional Documents
Culture Documents
WIT COMP1050: Inheritance and Polymorphism
WIT COMP1050: Inheritance and Polymorphism
WIT COMP1050
COMP1050 2 Fall
Wentworth Institute of
Technology Engineering & Technology
Cat Dog
-name: String -name: String
-id: String -id: String
-hairBalls: boolean
+Cat(name:String,
+Dog(name:String,
id:String,
id:String)
hairBalls:boolean)
+getName(): String
+getName(): String
+getId(): String
+getId(): String
+says(): String
+says(): String
+hasHairBalls(): boolean
COMP1050 3 Fall
Wentworth Institute of
Technology Engineering & Technology
Code (1)
Cat.java Dog.java
final private String name; final private String name;
final private String id; final private String id;
final private boolean hairBalls;
public Cat(String name, String id, boolean hairBalls) { public Dog(String name, String id) {
this.name = name; this.name = name;
this.id = id; this.id = id;
this.hairBalls = hairBalls; }
}
COMP1050 4 Fall
Wentworth Institute of
Technology Engineering & Technology
Code (2)
Vet.java Output
Dog[] dogs = { Spot (1234) says 'woof'
new Dog("Spot", "1234"), Rover (6789) says 'woof'
new Dog("Rover", "6789") Mittens (5432) says 'meow' :: CLEARS THROAT ::
}; Garfield (8765) says 'meow'
Cat[] cats = {
new Cat("Mittens", "5432", true),
new Cat("Garfield", "8765", false)
};
COMP1050 5 Fall
Wentworth Institute of
Technology Engineering & Technology
Alarms!
This code works, but there is a great deal of
duplication (which, aside from wasted effort,
often leads to mistakes/bugs)
Inheritance in OOP allows us to create general
classes (superclass), share this code amongst
other classes (subclass), and then specialize
them as needed
Polymorphism allows us to write code that
easily works with different subclasses that share
a common superclass
COMP1050 6 Fall
Wentworth Institute of
Technology Engineering & Technology
Inheritance Visually
Pet
-name: String
-id: String
+Pet(name: String,
id:String)
+getName(): String
+getId(): String
+says(): String
Cat Dog
-hairBalls: boolean
+Dog(name:String,
+Cat(name:String, id:String)
id:String, hairBalls:boolean)
+hasHairBalls(): boolean
COMP1050 7 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 8 Fall
Wentworth Institute of
Technology Engineering & Technology
Example (1)
Pet.java Dog2.java
final private String name; public class Dog2 extends Pet {
final private String id; public Dog2(String name, String id) {
super(name, id);
public Pet(String name, String id) { }
this.name = name; }
this.id = id;
}
COMP1050 9 Fall
Wentworth Institute of
Technology Engineering & Technology
Example (2)
Pet.java Cat2.java
final private String name; public class Cat2 extends Pet {
final private String id; final private boolean hairBalls;
Example (3)
Pet.java Test.java
final private String name; Cat2 c = new Cat2("Mittens", "5432", true);
final private String id; System.out.printf("%s%n", c);
System.out.printf("%b%n", c.hasHairBalls());
public Pet(String name, String id) {
this.name = name;
this.id = id;
}
COMP1050 11 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 12 Fall
Wentworth Institute of
Technology Engineering & Technology
Constructor Chaining
A constructor may invoke an overloaded constructor
(this(…)) or its superclass’s constructor (super(…))
» Either of which must be the first line of the constructor
If none of them is invoked explicitly, the compiler puts
super() as the first statement in the constructor
» Meaning, the superclass constructor is always invoked
Thus, constructing an instance of a class invokes all
the superclasses’ constructors along the inheritance
chain – this is known as constructor chaining
» Intuition: set up the more general class first before
initializing the specialization
COMP1050 14 Fall
Wentworth Institute of
Technology Engineering & Technology
Example (1)
Super.java Sub.java
public class Super { public class Sub extends Super {
public Super() { public Sub() {
System.out.printf("Super()%n"); this("");
} System.out.printf("Sub()%n");
} }
public Sub(String s) {
System.out.printf("Sub(s)%n");
}
Super()
Sub(s)
Sub()
COMP1050 15 Fall
Wentworth Institute of
Technology Engineering & Technology
Example (1a)
Super.java Sub.java
public class Super { public class Sub extends Super {
public Super() { public Sub() {
System.out.printf("Super()%n"); this("");
} System.out.printf("Sub()%n");
} }
public Sub(String s) {
// super();
System.out.printf("Sub(s)%n");
}
Super()
Sub(s)
Sub()
COMP1050 16 Fall
Wentworth Institute of
Technology Engineering & Technology
Example (1b)
Super.java Sub.java
public class Super { public class Sub extends Super {
public Super() { public Sub() {
System.out.printf("Super()%n"); this("");
} System.out.printf("Sub()%n");
} }
public Sub(String s) {
super();
System.out.printf("Sub(s)%n");
}
Super()
Sub(s)
Sub()
COMP1050 17 Fall
Wentworth Institute of
Technology Engineering & Technology
public Sub(String s) {
System.out.printf("Sub(s)%n");
}
COMP1050 18 Fall
Wentworth Institute of
Technology Engineering & Technology
public Sub(String s) {
System.out.printf("Sub(s)%n");
}
COMP1050 19 Fall
Wentworth Institute of
Technology Engineering & Technology
public Sub(String s) {
System.out.printf("Sub(s)%n");
}
COMP1050 20 Fall
Wentworth Institute of
Technology Engineering & Technology
public Sub(String s) {
System.out.printf("Sub(s)%n");
}
Super()
COMP1050 21 Fall
Wentworth Institute of
Technology Engineering & Technology
public Sub(String s) {
System.out.printf("Sub(s)%n");
}
Super()
Sub(s)
COMP1050 22 Fall
Wentworth Institute of
Technology Engineering & Technology
public Sub(String s) {
System.out.printf("Sub(s)%n");
}
Super()
Sub(s)
Sub()
COMP1050 23 Fall
Wentworth Institute of
Technology Engineering & Technology
Example (2)
Super.java Sub.java
public class Super { public class Sub extends Super {
public Super() { public Sub() {
System.out.printf("Super()%n"); this("");
} System.out.printf("Sub()%n");
} }
public Sub(String s) {
System.out.printf("Sub(s)%n");
}
Super()
Sub(s)
COMP1050 24 Fall
Wentworth Institute of
Technology Engineering & Technology
Exercise – Output?
public class Faculty extends Employee {
public Faculty() {
System.out.printf("Faculty()%n");
}
public Employee(String s) {
System.out.printf("%s%n", s);
} Person()
}
Employee(s)
public class Person {
public Person() { Employee()
}
System.out.printf("Person()%n");
Faculty()
}
COMP1050 25 Fall
Wentworth Institute of
Technology Engineering & Technology
public Employee(String s) {
System.out.printf("%s%n", s);
}
}
COMP1050 26 Fall
Wentworth Institute of
Technology Engineering & Technology
public Employee(String s) {
System.out.printf("%s%n", s);
}
}
COMP1050 27 Fall
Wentworth Institute of
Technology Engineering & Technology
public Employee(String s) {
System.out.printf("%s%n", s);
}
}
COMP1050 28 Fall
Wentworth Institute of
Technology Engineering & Technology
public Employee(String s) {
System.out.printf("%s%n", s);
}
}
COMP1050 29 Fall
Wentworth Institute of
Technology Engineering & Technology
public Employee(String s) {
System.out.printf("%s%n", s);
}
}
COMP1050 30 Fall
Wentworth Institute of
Technology Engineering & Technology
public Employee(String s) {
System.out.printf("%s%n", s);
} Person()
}
COMP1050 31 Fall
Wentworth Institute of
Technology Engineering & Technology
public Employee(String s) {
System.out.printf("%s%n", s);
} Person()
}
Employee(s)
public class Person {
public Person() {
System.out.printf("Person()%n");
}
}
COMP1050 32 Fall
Wentworth Institute of
Technology Engineering & Technology
public Employee(String s) {
System.out.printf("%s%n", s);
} Person()
}
Employee(s)
public class Person {
public Person() { Employee()
System.out.printf("Person()%n");
}
}
COMP1050 33 Fall
Wentworth Institute of
Technology Engineering & Technology
public Employee(String s) {
System.out.printf("%s%n", s);
} Person()
}
Employee(s)
public class Person {
public Person() { Employee()
}
System.out.printf("Person()%n");
Faculty()
}
COMP1050 34 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 35 Fall
Wentworth Institute of
Technology Engineering & Technology
Answer
Apple has default no-arg constructor,
which tries to call super()
Fruit does not have a no-arg constructor!
COMP1050 36 Fall
Wentworth Institute of
Technology Engineering & Technology
Exercise – Output?
public class COMP1050 {
public static void main(String[] args) {
System.out.printf("before");
new CS();
System.out.printf(".after");
}
}
COMP1050 37 Fall
Wentworth Institute of
Technology Engineering & Technology
Answer
before.wit.cs.after
COMP1050 38 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 40 Fall
Wentworth Institute of
Technology Engineering & Technology
Java Annotations
Annotations are a form of metadata – they provide
data about a program that is not part of the program
itself
Annotations have no direct effect on the operation of
the code they annotate
» Useful for detecting/ignoring errors
Examples…
» @SuppressWarnings("resource")
Ignores warning on unclosed Scanner on System.in
» @Override
Compiler error if not overriding an existing method
COMP1050 41 Fall
Wentworth Institute of
Technology Engineering & Technology
Overriding says()
Pet.java Dog3.java
final private String name; public class Dog3 extends Pet {
final private String id; public Dog3(String name, String id) {
super(name, id);
public Pet(String name, String id) {
}
this.name = name;
this.id = id;
@Override
}
public String says() {
public String getName() { return "woof";
return name; }
}
public static void main(String[] args) {
public String getId() { System.out.printf("%s%n",
return id; new Dog3("Lassie", "124").says());
}
}
}
public String toString() {
return String.format("%s (%s)", getName(), getId());
} woof
public String says() {
return "???";
}
COMP1050 42 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 44 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 45 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 46 Fall
Wentworth Institute of
Technology Engineering & Technology
Visibility Modifiers
Way of specifying what code can “see” (i.e.
directly access) a variable/method
» By default, operate under a “need-to-know”
basis (i.e. most constraining)
» No modifier = package-private
Class Package Subclass World
public ✓ ✓ ✓ ✓
protected ✓ ✓ ✓
no modifier ✓ ✓
private ✓
COMP1050 47 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 48 Fall
Wentworth Institute of
Technology Engineering & Technology
Example
Super.java Sub.java
public static void m1() { public class Sub extends Super {
System.out.printf("Super.m1()%n");
} public static void m2() {
System.out.printf("Sub.m2()%n");
public static void m2() { }
System.out.printf("Super.m2()%n");
}
public static void main(String[] args) {
Super.m1();
Super.m2();
Sub.m1();
Sub.m2();
}
} Super.m1()
Super.m2()
Super.m1()
Sub.m2()
COMP1050 49 Fall
Wentworth Institute of
Technology Engineering & Technology
10 20
COMP1050 50 Fall
Wentworth Institute of
Technology Engineering & Technology
20 20
COMP1050 51 Fall
Wentworth Institute of
Technology Engineering & Technology
Exercise
What are the effects of keeping all of a
class’ constructors private?
COMP1050 52 Fall
Wentworth Institute of
Technology Engineering & Technology
Answer
No one outside of the class can
instantiate the class
The class can not be inherited!
COMP1050 53 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 54 Fall
Wentworth Institute of
Technology Engineering & Technology
Getting Abstract
An abstract method is a method that is declared
without an implementation
abstract public String says();
If a class has even one abstract method, then the class
itself must be declared abstract
abstract public class Pet2
An abstract class is a class that is declared abstract (it
may or may not include abstract methods)
» Abstract classes cannot be instantiated
» Abstract classes can be subclassed
» The opposite of an abstract class is a “concrete” class
COMP1050 56 Fall
Wentworth Institute of
Technology Engineering & Technology
}
this.id = id;
Any concrete subclass
public String getName() { of Pet2 must
return name;
} implement says()
public String getId() {
return id;
}
@Override
public String toString() {
return String.format("%s (%s)", getName(), getId());
}
abstract public String says();
}
COMP1050 57 Fall
Wentworth Institute of
Technology Engineering & Technology
@Override
public String says() {
return "meow";
}
COMP1050 58 Fall
Wentworth Institute of
Technology Engineering & Technology
Abstract Visual
Pet2
-name: String
-id: String
+Pet(name: String,
id:String)
+getName(): String
+getId(): String
+says(): String
Cat4 Dog4
-hairBalls: boolean
+Cat4(name:String, +Dog4(name:String,
id:String, id:String)
hairBalls:boolean) +says(): String
+says(): String
+hasHairBalls(): boolean
COMP1050 59 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 60 Fall
Wentworth Institute of
Technology Engineering & Technology
Polymorphism
Polymorphism: a variable of a supertype can
refer to a subtype object
A class defines a type
A type defined by a subclass is called a
subtype; a type defined by its superclass is
called a supertype
» Example: Dog4 is a subtype of Pet2; Pet2 is a
supertype of Cat4
Object is the supertype of all classes
COMP1050 61 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 62 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 63 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 64 Fall
Wentworth Institute of
Technology Engineering & Technology
Dynamic Binding
When an object’s method is invoked, the
implementation that is used will be determined
by the JVM at runtime – this capability is known
as dynamic binding
» Whereas overloading can be achieved at compile-time,
overriding must occur at run-time
The JVM searches the implementations for the method
from the most specific to the most general
Once an implementation is found, the search stops and
the first-found implementation is invoked
COMP1050 65 Fall
Wentworth Institute of
Technology Engineering & Technology
Example (1)
Polly.java
public class Polly { public static void p(Object o) {
} System.out.printf("%s%n", o.toString());
}
Polly@70dea4e
COMP1050 66 Fall
Wentworth Institute of
Technology Engineering & Technology
Example (2)
Polly.java
public class Polly { public static void p(Object o) {
@Override System.out.printf("%s%n", o.toString());
public String toString() { }
return "wants a cracker";
} public static void main(String[] args) {
} p(new Polly());
}
wants a cracker
COMP1050 67 Fall
Wentworth Institute of
Technology Engineering & Technology
C1@70dea4e
C2.toString()
C2.toString()
COMP1050 68 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 69 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 70 Fall
Wentworth Institute of
Technology Engineering & Technology
Exercise – Output?
public class A { public static void main(String[] args) {
protected int i = 7; new A();
new B();
public A() { }
setI(20);
System.out.printf("A(): i=%d%n", i);
}
COMP1050 72 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 73 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 74 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 75 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 76 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 77 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 78 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 79 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 80 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 81 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 82 Fall
Wentworth Institute of
Technology Engineering & Technology
Object o1 = "howdy";
boolean isS = o1 instanceof String; // true
boolean isD = o1 instanceof Double; // false
COMP1050 84 Fall
Wentworth Institute of
Technology Engineering & Technology
Compare to…
Vet.java Spot (1234) says 'woof'
Dog[] dogs = {
new Dog("Spot", "1234"), Rover (6789) says 'woof'
new Dog("Rover", "6789") Mittens (5432) says 'meow' :: CLEARS THROAT ::
};
Garfield (8765) says 'meow'
Cat[] cats = {
new Cat("Mittens", "5432", true),
new Cat("Garfield", "8765", false)
};
COMP1050 87 Fall
Wentworth Institute of
Technology Engineering & Technology
COMP1050 88 Fall
Wentworth Institute of
Technology Engineering & Technology
System.out.printf("%b%n", d1.equals(d2));
System.out.printf("%b%n", d1.equals(lost));
System.out.printf("%b%n", d2.equals(lost));
false
false
false
COMP1050 89 Fall
Wentworth Institute of
Technology Engineering & Technology
false
true
false
COMP1050 90 Fall
Wentworth Institute of
Technology Engineering & Technology