1 of 46

Inheritance Review

Modified from O(N)CS Lessons

2 of 46

To do: October 12

Take out your tests - be ready to turn in

CSAwesome online textbook -> Create an account https://runestone.academy/runestone

Then enroll in a course with course name: CSIII21-22

3 of 46

Warm-up #1: Predict what will happen with code snippet below.

int x = 10;

double y = 11;

int temp = x;

x = y;�y = temp;

System.out.println(x + " " + y);

4 of 46

Warm-up #2: How can you fix this error?

int x = 10;

double y = 11;

int temp = x;

x = y;�y = temp;

5 of 46

Warm-up #2: How can you fix this error?

int x = 10;

double y = 11;

int temp = x;

x = (int)y;�y = temp;

6 of 46

Go over homework

  • Test corrections
  • Big O packet

7 of 46

Draw the class diagram showing relationship between Object and String classes

8 of 46

Object class…back to the beginning

Before moving on, we have to go back to the beginning, back to the motherland, the origin of all objects, the Object class!�• The class defined as Object is the most basic class there is.�• All other classes are based on this.

9 of 46

Object Class

An instance of the String class is also an instance of the Object class

• All Java class definitions originate from the Object class�

10 of 46

Object class

Here is the basic class definition for the Object class, as described in the Java documentation:

public class Object�Class Object is the root of the class hierarchy.�Every class has Object as a superclass. All objects, including arrays, implement the methods of this class. �

11 of 46

API for Object class

12 of 46

Object construction

The Object class has only one constructor:

Object()

13 of 46

Object methods

And has several methods including

boolean equals (Object obj) …

Indicates whether some other object is "equal to" the�current one.

String toString () …

Returns a string representation of this object.

14 of 46

In CodePad

Object obj = new Object();�System.out.println(obj);

OUTPUT

15 of 46

In CodePad

Object obj = new Object();�System.out.println(obj);

OUTPUT

java.lang.Object@109e4d8

16 of 46

In CodePad

Object obj1 = new Object();�Object obj2 = new Object();�System.out.println(obj1 == obj2);

OUTPUT

17 of 46

In CodePad

Object obj1 = new Object();�Object obj2 = new Object();�System.out.println(obj1 == obj2);

OUTPUT

false

18 of 46

String class

Now that we know the origin of objects, let’s return to the String class, which is a subclass or child class, of the Object class.�• The Object class is a superclass, or parent of the String class

19 of 46

Inheritance

The String class “inherits” all the characteristics of the Object class, namely the methods.�• This means the String class owns all of the same methods as the Object class, including equals() and toString().�

20 of 46

Inheritance

Of course, it also has several methods of its own, in addition to the Object ones, such as... ?

21 of 46

Inheritance

Of course, it also has several methods of its own, in addition to the Object ones, such as length(), substring(), charAt(), and indexOf().

22 of 46

Examples

Let’s look at a program that will explore the relationship between Objects and Strings…

23 of 46

Sample Program

Can an Object object variable can “reference” or “point to” a String?

Object obj = new String("hello");

24 of 46

In CodePad

Object obj = new String("hello");�System.out.println(obj);

OUTPUT

25 of 46

In CodePad

Object obj = new String("hello");�System.out.println(obj);

OUTPUT

hello

26 of 46

Inheritance

When an Object variable is assigned to a String, the string returned by the toString method is different.

An Object variable can point to a String object with no problem. We also see that the toString method already defined in the Object class was OVERRIDDEN by the toString method of the String class.

27 of 46

Another example

We just saw that on Object variable can point to a String object with no problem.�• Can a String reference variable be assigned an Object instance?

28 of 46

In CodePad

Object obj = new String("hello");�String s = obj;�System.out.println(s);

OUTPUT

29 of 46

In CodePad

Object obj = new String("hello");�String s = obj;�System.out.println(s);

30 of 46

In CodePad

Object obj = new String("hello");�String s = obj; // ERROR!�System.out.println(s);

31 of 46

Example

Even though obj is pointing to a String object, the String reference variable s cannot point to the same String object that obj is pointing to!�Why not!

32 of 46

Example

Since the Object variable can point to ANY object (since all classes are subclasses of the Object class), the compiler cannot guarantee that the Object variable points to a String (even though it does in this case).

Therefore, it will not allow this assignment to occur, �

33 of 46

Casting

How do we get around this problem?

Object obj = new String("hello");�String s = obj; �System.out.println(s);

34 of 46

Casting

We can get around this problem by casting!

Object obj = new String("hello");�String s = (String)obj;�System.out.println(s);

Now it works!

35 of 46

Overriding the toString() method

The String class has a toString() method that overrides the toString() method from the Object class.

When we write a class, we generally want to override the accessor method toString() which will allow us to customize the output for our object.

36 of 46

Class diagram of relationship between Object and String

37 of 46

Predict what will be returned by the substring method:

String str = "hello";�Object obj = str;�str.substring(0,2) → �obj.substring(0,2) →

38 of 46

Predict what will be returned by the substring method:

String str = "hello";�Object obj = "hello";

str.substring(0,2) → "he"�obj.substring(0,2) Error: cannot find symbol - method substring()

39 of 46

NO substring method in Object class!

40 of 46

Parent class variable issue

Because obj variable is declared as an Object type even the the object obj is pointing to is a String object variable, the substring method is NOT defined in the Object class.

Look up Object class documentation and look over the methods defined in the class.

41 of 46

Predict what will be returned by calls below:

Object obj1 = new Object();

Object obj2 = new Object();

1. obj1 == obj2 →

2. obj1.equals(obj2) →

String str1 = new String("hi");

String str2 = new String("hi");

3. str1 == str2 →

4. str1.equals(str2) →

42 of 46

Predict what will be returned by calls below:

Object obj1 = new Object();

Object obj2 = new Object();

1. obj1 == obj2 → false

2. obj1.equals(obj2) → false

String str1 = new String("hi");

String str2 = new String("hi");

3. str1 == str2 → false

4. str1.equals(str2) → true

43 of 46

Predict what will be returned by calls below:

Object obj1 = new String("hi");

Object obj2 = new String("hi");

1. obj1.equals(obj2) →

2. obj1 == obj2 →

44 of 46

Predict what will be returned by calls below:

Object obj1 = new String("hi");

Object obj2 = new String("hi");

1. obj1.equals(obj2) → true

2. obj1 == obj2 → false

Why? equals method is overridden in String class and executed when actual object is a String.

45 of 46

46 of 46

Homework due Friday

  • Complete Inheritance Review worksheet and submit to Hub
  • CSAwesome 9.1-9.3 Inheritance Review part 1 - readings and exercises