CS62 - Spring 2021 - Class 4
Example code in this lecture
Inheritance
Lecture notes
administrative
- quiz 1 today!
- assignment 2: how's it going?
inheritance
- we can build a new class that is an extension of another class
- the original class is said to be the superclass (or parent class) and the new class the subclass (or child class)
- to do this, in the class header, you use the keyword "extends", e.g.
public class B extends A{
...
}
- the subclass:
- inherits all public methods from the superclass
- e.g. given an object of type B, we can call any method that A had on B
- can access any instance variables that are declared public *or* protected
- can call any methods that are declared as public *or* protected
- additions in the subclass
- you can then add any additional instance variables and methods in the subclass
- these will be available normally in the subclass, but NOT in the superclass
- if you define a method with the same "signature" as in the superclass, it will override it
protected
- labeling a variable as protected allows the child class to access that variable, but no other classes
- labeling a method as protected allows the child class to access those methods, but no other classes
the Java class hierarchy
- *everything* in Java is a subclass of some class (except for 1 class in Java)
- you can only inherit from one class
- if you don't inherit from a class, by default, you inherit from the class Object
- You can see the object hierarchy at:
https://docs.oracle.com/javase/8/docs/api/java/lang/package-tree.html
Look at
Inheritance code
- We have a class called A that has a private and protected instance variables
- Two public methods
- B is a class that inherits from A
- we indicate this with the keyword "extends"
- B has all of the instance variables of A
- however, it can only access those that are protected or public
- in the resetInt method, we can access number
- if we tried to access the text variable, we would get an error
super
- if the superclass has a constructor that has parameters, then it needs information to be constructed
- when a subclass extends a superclass, it, in essence, has a version of the superclass nested inside of it
- because of this, when constructing the subclass, the superclass also need to be constructed
- to call the superclass's construct, you use the keyword "super"
- it has to be the *first* thing that happens in the constructor of the sublcass
Variable type
- the type of a variable defines what methods we can call on the associated object:
A blah = ...;
// some code
// What methods can I call on blah?
- only the public methods of A
- Why is it ok to assign a B object to a variable of type A?
- A blah = new B(...);
- What methods can we call on blah?
- only the public methods of A?
- Does B have all of those methods?
- Yes!
- Will they have the same functionality as A's public methods?
- not if B overrode them, i.e., wrote a method with the same type signature
types
- an A object is of type A
- a B object is of type B
- a B object can also of type A! (remember it has an A inside it and all of the functionality of an A)
- For example, the following are all legal:
A temp = new A(10, "blah");
B temp = new B(10, "blah");
A temp = new B(10, "blah");
- but the following is not:
B temp = new A(10, "blah");
look at the test method in the B class in
Inheritance code
- What will be printed out when we run it?
10
0
10
- notice that the B class inherits all of the public methods from the A class
- specifically, we call the getNumber method and it gets the method description from the A class
- also note that even though we do not have any instance variables declared, we inherit all the instance variables from A (number and text)
- If we tried to call the resetInt method on an A object, it won't work!
look at test2
- What will be printed out?
test
- We've declared a variable of type A, why can we assign a B object to it?
- All B object have the A methods!
- Why can't we can't the resetInt method?
- Even though it's a B object (and we know it), the Java compiler can't guarantee it:
For example:
A a;
if( rand.randInt(2) == 1 ){
a = new A(10, "test");
}else{
a = new B(10, "test");
}
// we have no idea if a references an A or a B!
look at test3 in the B class in
Inheritance code
- What will be printed out?
A1: I'm an A
B1: I'm a B
A2: I'm a B
- Since A2 references an object of type B, then B's toString method gets called!
- When the methods are called, they are based on the object
look at the shape package in the
Inheritance code
- The Shapes class defines basic behavior
- The Square and Rectangle classes extend the Shape class
- they both override the getArea method
- they both add methods particular to the class
look at ShapeMaker class in
Inheritance code
- What does the getShapes method do?
- prompts the user for a number
- then prompts them for that number of shapes
- if the user enters a rectangle or square it creates one
- otherwise creates a generic shape
- Where does it store these shapes?
- in an ArrayList of Shapes
- Why does that work?
- Rectangles and Squares are both shapes!
- What does the printShapes method do?
- goes through the Shape ArrayList and prints each shape out (i.e. calls the toString method)