CPS 209 Lecture 1: Lecture 1 Inheritance And Polymorphism

127 views25 pages
Lecture 1: Inheritance And Polymorphism
lecture
Files used: inheritancedemo.java, Barnyard.java, CollectionsDemo.java
1.1. The need for inheritance
The DRY Principle (Don’t Repeat Yourself):
It instructs us to never say anything twice in the same program.
What is related to the DRY principle?
Single Point Of Truth Principle
It states that every contingent truth about the behaviour of the program should be stated
exactly in one place inside the program.
Why is inheritance needed?
Organization: it is used to organize classes
Expressing different types:
Without it, if there are only classes used to express types
it is hard to compare 2 problem domains to see if they are conceptually closer than another 2.
Eg: problem domain concepts of Hawk and Sparrow are somehow conceptually “closer” to
each other than problem domain concepts of Hawk and Planet.
To write a Polymorphic method:
it is a method whose parameter type is a superclass
Without inheritance + static type checking this is not possible
// Example: If there was NO inheritance
// You could write two identical methods for separate birds
// this would satisfy type-checking
// but it would cause SO much additional code (completely against our DRY principle)
void pluck(Hawk h){
...
}
void pluck(Sparrow h){
...
}
// So with inheritance we can make this 1 method!
void foo (Bird b){
// it could be given an argument of any Bird, but not a planet or a car
}
// The method could be made more liberal - accept any object as an argument - even if you w
ant to only use it for birds
// But this would cause a runtime crash: AKA during program execution
// This could be problematic if you're showing a customer the product, etc
// eg: calling this method with a String or a BankAccount object
void pluck(Hawk h){
// anything could be passed into this
// even a non-bird object
// can cause a runtime crash if a non-bird object is used to call it's methods
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 25 pages and 3 million more documents.

Already have an account? Log in
Java’s Static type checking:
exists to prevent runtime crashes due to the program trying to do something logically impossible
eg: asking a bank account to fly or trying to deposit a hundred dollars into an owl
very strong
it is impossible to create a situation where the program crashes at runtime because of trying to call a
method that doesn’t exist in the object.
The program can still crash because of various other reasons that cannot be detected during
compilation, even in principle.
Dynamic languages such as Python or Ruby:
no static type system within the language itself
Note: their objects do still have types in the virtual machine, but these are not explicitly used while
typing in these languages
has more situations where the program could crash at runtime while trying to call a method that
doesn’t exist in the object
this is the cost of the extra flexibility of the duck typing in line of the dynamic philosophy of those
languages.
Solution: Heavy use of unit testing alleviates this problem.
}
// INHERITANCE DEMO
// File: inheritancedemo.java
// This doesn't do anything useful yet, just demonstrates the language.
public class InheritanceDemo {
// When a nested class is declared static, objects of that class can
// exist on their own, without an outer class context object.
private static class A { // superclass
public int x; // can be accessed everywhere
private int y; // can be accessed in same class only (cannot be accessed from the s
ubclasses)
protected int z; // can be accessed by same class + subclasses
public A() { // Constructor - initializes fields
System.out.println("Executing constructor of A");
x = 17; y = 42; z = 99;
}
public void outputValues() {
System.out.println("x is " + x);
System.out.println("y is " + y);
System.out.println("z is " + z);
}
public void greet() {
System.out.println("Hello");
}
}
private static class B extends A { // B is the subclass of superclass A; inherits it's
26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 25 pages and 3 million more documents.

Already have an account? Log in
Let’s look at the main function’s references:
Stack Heap
A: first ———————> refers to the object A here
x = 17
y = 42
z = 99
A: second ——————> refers to the object B here (inherited A fields + B’s fields)
x = 17
y = 42
z = 99
w = 123
x = -999
B: third ——————> refers to the object B here (inherited A fields + B’s fields)
x = 17
y = 42
members (except Object class)
public int w; // adding new fields, these don't exist in the superclass
public int x; // (yes, even this is possible, but bad naming convention)
public B() { // subclass Constructors are executed as chain down the line.
System.out.println("Executing constructor of B");
w = -123; x = -999;
}
@Override // using this lets the compiler what to expect, so it can catch exception
s
// methods from the superclass must be inherited by the subclass, but if you don't
want it to work
// you can redefine the method by overriding it, so it works a different way
public void outputValues() { // overriding an inherited method
super.outputValues(); // we first call the superclass version of method
System.out.println("w is " + w); // then output the new field
System.out.println("x defined in B is " + x);
System.out.println("x inherited from A is " + super.x);
// the only way to access a private superclass field, eg: y
// you have to call an instance method of the superclass
// use a keyword called super: super.outputValues();
}
@Override
public void greet() { // overriding an inherited method
System.out.println("Hi there");
// Note that in this method, we don't call the superclass version.
}
public void bye() { // defining whole new methods in the subclass
System.out.println("Bye");
}
}
}
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 25 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+
$10 USD/m
Billed $120 USD annually
Homework Help
Class Notes
Textbook Notes
40 Verified Answers
Study Guides
1 Booster Class
Class+
$8 USD/m
Billed $96 USD annually
Homework Help
Class Notes
Textbook Notes
30 Verified Answers
Study Guides
1 Booster Class