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
Object
: The superclass of all classes
All classes extend
Object
Most useful methods:
String toString()
boolean equals(Object otherObject)
Object clone()


Page 45
Object
: toString()
Returns a string representation of the object
Useful for debugging
Example: toString() in class Rectangle returns something like:
java.awt.Rectangle[x=5,y=10,width=20,height=30]
toString() used by concatenation operator:
BankAccount
b = new
BankAccount(9876543,300);
String test = “xyz” +
b; // means
String test = “xyz” + b.toString();
toString() in class Object returns class name and object address:
Employee@d2460bf


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


Page 47
class Object: equals(Object other)
equals() tests for equal
contents
. 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;
}
}
NOTE:
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();
exec1.setName(“bossman”);
exec1.setPayRate(150.0);
Executive exec2 = new Executive();
exec2.setName(“bigger bossman”);
exec2.setPayRate(250.0);
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
must
implement method area()


Ace your assessments! Get Better Grades
Browse thousands of Study Materials & Solutions from your Favorite Schools
Ryerson University
Ryerson_University
School:
Computer_Science_II
Course:
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