what is object type casting? give some example with related?
Answer Posted / ganesh
In java object typecasting one object reference can be type
cast into another object reference. The cast can be to its
own class type or to one of its subclass or superclass types
or interfaces. There are compile-time rules and runtime
rules for casting in java.
Consider an interface Vehicle, a super class Car and its
subclass Ford. The following example shows the automatic
conversion of object references handled by the compiler
interface Vehicle {
}
class Car implements Vehicle {
}
class Ford extends Car {
}
Let c be a variable of type Car class and f be of class Ford
and v be an vehicle interface reference. We can assign the
Ford reference to the Car variable:
I.e. we can do the following
'''Example 1
c = f; //Ok Compiles fine'''
Where c = new Car();
And, f = new Ford();
The compiler automatically handles the conversion
(assignment) since the types are compatible (sub class -
super class relationship), i.e., the type Car can hold the
type Ford since a Ford is a Car.
| Is This Answer Correct ? | 6 Yes | 0 No |
Post New Answer View All Answers
What is meant by method overriding?
What is a boolean in java?
Differences between traditional programming language and object oriented programming language?
What is garbage collection? What is the process that is responsible for doing that in java?
How to create an immutable class?
What is a stack class in java ?
How do we access static members in java?
Write a method that will remove given character from the string?
Explain tree set and its features?
what is the difference between future and callable interface in java?
What interface is extended by awt event listeners?
Why stringbuilder is not thread safe in java?
Do we need to manually write Copy Constructor?
What about member inner classes?
What is abstraction in java?