Inheritance Review
Modified from O(N)CS Lessons
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
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);
Warm-up #2: How can you fix this error?
int x = 10;
double y = 11;
int temp = x;
x = y;�y = temp;
Warm-up #2: How can you fix this error?
int x = 10;
double y = 11;
int temp = x;
x = (int)y;�y = temp;
Go over homework
Draw the class diagram showing relationship between Object and String classes
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.
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�
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. �
API for Object class
Object construction
The Object class has only one constructor:
Object()
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.
In CodePad
Object obj = new Object();�System.out.println(obj);
OUTPUT
In CodePad
Object obj = new Object();�System.out.println(obj);
OUTPUT
java.lang.Object@109e4d8
In CodePad
Object obj1 = new Object();�Object obj2 = new Object();�System.out.println(obj1 == obj2);
OUTPUT
In CodePad
Object obj1 = new Object();�Object obj2 = new Object();�System.out.println(obj1 == obj2);
OUTPUT
false
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
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().�
Inheritance
Of course, it also has several methods of its own, in addition to the Object ones, such as... ?
Inheritance
Of course, it also has several methods of its own, in addition to the Object ones, such as length(), substring(), charAt(), and indexOf().
Examples
Let’s look at a program that will explore the relationship between Objects and Strings…
Sample Program
Can an Object object variable can “reference” or “point to” a String?
Object obj = new String("hello");
In CodePad
Object obj = new String("hello");�System.out.println(obj);
OUTPUT
In CodePad
Object obj = new String("hello");�System.out.println(obj);
OUTPUT
hello
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.
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?
In CodePad
Object obj = new String("hello");�String s = obj;�System.out.println(s);
OUTPUT
In CodePad
Object obj = new String("hello");�String s = obj;�System.out.println(s);
In CodePad
Object obj = new String("hello");�String s = obj; // ERROR!�System.out.println(s);
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!
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, �
Casting
How do we get around this problem?
Object obj = new String("hello");�String s = obj; �System.out.println(s);
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!
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.
Class diagram of relationship between Object and String
Predict what will be returned by the substring method:
String str = "hello";�Object obj = str;��str.substring(0,2) → �obj.substring(0,2) →
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()
NO substring method in Object class!
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.
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) →
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
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 →
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.
Homework due Friday