2Inheritance.pdf-CPS209: Computer Scienc...
2Inheritance.pdf-CPS209: Computer Science II Inheritance Inheritance
Showing 44-49 out of 50
2Inheritance.pdf-CPS209: Computer Science II Inher...
2Inheritance.pdf-CPS209: Computer Science II Inheritance Inheritance
2Inheritance.pdf-CPS209: Computer S...
2Inheritance.pdf-CPS209: Computer Science II Inheritance Inheritance
Page 44
: The superclass of all classes
All classes extend
Most useful methods:
String toString()
boolean equals(Object otherObject)
Object clone()

Page 45
: toString()
Returns a string representation of the object
Useful for debugging
Example: toString() in class Rectangle returns something like:
toString() used by concatenation operator:
b = new
String test = “xyz” +
b; // means
String test = “xyz” + b.toString();
toString() in class Object returns class name and object address:

Page 46
: The superclass of all classes
It is common to override toString():
public class Employee
public String
return "Employee[name=" + name +
“ "
+ "payRate=” +
+ "]";
. . .

Page 47
class Object: equals(Object other)
equals() tests for equal
. Examples:
BankAccount => compare balance
Coin => compare value
Employee => compare name and/or payRate
== tests for equal memory locations of objects!!
Must cast the “Object other” parameter to subclass
public class Employee
public boolean equals(Object other)
Employee otherEmpl = (Employee) other;
return name.equals(otherEmpl.name) && payRate == otherEmpl.payRate;
Strings are objects so must use equals method in class String to compare two strings!!!
Cannot just write:
return name == otherEmpl.name
&& payRate == otherEmpl.payRate;

Page 48
class Object: equals(Object other)
public class ExecutiveTester
public static void main(String[] args)
Executive exec1 = new Executive();
Executive exec2 = new Executive();
exec2.setName(“bigger bossman”);
if (exec1 == exec2)
System.out.println(“two references to same object”);
if (exec1.equals(exec2))
System.out.println(“two objects with same name and payrate”);

Page 49
Abstract Methods and Classes
When you extend a class, you have choice whether to override a
method or not.
Sometimes want to force the subclass creator to override a method
(method might be common to all subclasses so should define it in
superclass but there is no good default implementation) Example:
area() method of superclass Shape and subclasses (Circle,Rectangle)
You can make the method abstract (just signature, no body):
abstract public double area()
Now subclass programmer
implement method area()

Ace your assessments! Get Better Grades
Browse thousands of Study Materials & Solutions from your Favorite Schools
Ryerson University
Great resource for chem class. Had all the past labs and assignments
Leland P.
Santa Clara University
Introducing Study Plan
Using AI Tools to Help you understand and remember your course concepts better and faster than any other resource.
Find the best videos to learn every concept in that course from Youtube and Tiktok without searching.
Save All Relavent Videos & Materials and access anytime and anywhere
Prepare Smart and Guarantee better grades

Students also viewed documents