Casting Object References
Superclass:
Object can hold the reference of its subclass
Object reference can call a method of the subclass that does
not exist in it
Object reference must be cast to its subclass type to call the
subclass method, as shown in the following code snippet:
public void modifyDeptForManager (Employee e,
String dept) {
if (e instanceof Manager) {
Manager m = (Manager) e;
m.setDeptName(dept);
}
} Here, e is an Employee class object that holds the
reference to the Manager class and the setDeptName()
method of the Manager class is called.
Casting Rules
Casting can be of the following types:
Upward
Downward
The following figure depicts upward casting of the object.
Director d=new Director();
Manager m=new Manager();
Here, d is an object of the
Director class and m is an
Employee e = m; // OK object of the Manager class.
Employee e = d;//Ok
Manager m = d; // OK
Casting Rules (Contd.)
The following figure depicts downward casting of an object.
Casting
operator
Manager m = (Manager) e;
// Would also work if
// e was a Director obj
Director d = (Director) m; Engineer eng = (Engineer)m;
// fails at run time // compiler error
The downward cast is
The statement compiles, but invalid, as there is no
throws a runtime exception. hierarchical relationship
However, if m is an object of the between the Manager and
Director class, the downward the Engineer classes.
cast will succeed.
Overriding Object Methods
java.lang.Object class:
Parent of all Java classes by default
Contains several methods
Has the following important non-final methods that can be
overridden:
toString()
equals()
hashCode()
The following code snippets show the inheritance of the
Object class in a user-defined Employee class:
public class Employee { //... }
Or
public class Employee extends Object { //... }
Object toString() Method
toString()method:
Called to return the string value of an object
Can be overridden to provide instance information
The following code snippet shows how to override the
toString() method in the Employee class:
public class Employee{ Here, empId and name
//fields of Employee class are data fields of the
public String toString () { Employee class.
return "Employee id: " + empId + "\n"
"Employee name:" + name;
}
}
Object equals() Method
The equals()method of the Object class compares only
the object references.
If x and y are two objects of a class, then x is equal to y
if and only if x and y refer to the same object.
To test the contents of the objects, instead of their
references, override the equals()method.
Need for Generalization
While designing an object-oriented solution, code
duplication should be avoided.
Techniques to avoid code duplication are:
Create library methods and classes.
Use method inheritance.
Use generalization.
Enabling Generalization
Generalization:
Process of extracting shared characteristics from two or more
classes
Implemented through attributes, associations, or methods
The following figure depicts generalization.
The following code snippet shows the implementation of
generalization:
ElectronicDevice dev = new Television();
dev.turnOn();
// all ElectronicDevices can be turned on
Quiz (Contd.)
Fill in the blank:
________ is the process of extracting shared characteristics
from two or more classes.
Solution:
Generalization
Virtual Method Invocation:
________ is the process of extracting shared characteristics
from two or more classes.
Solution:
Generalization
Summary
In this session, you learned that:
Virtual method invocation is an aspect of polymorphism.
Using the most generic form of the object is a good practice of
polymorphism.
An object’s type can be determined at runtime using the
instanceof operator.
There two rules of casting objects:
Upward cast
Downward cast
The Object class is default parent class of all Java classes.
Code duplicity is a business problem, which can be avoided
through libraries, inheritance, and generalization.
Generalization is the process of extracting shared
characteristics from two or more classes.