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
Write a program to find maximum and minimum number in array?
Can a hashset contain duplicates java?
Is it safe to install java on my computer?
In how many ways we can create threads in java?
What is method in research paper?
Which sort is best in java?
What is immutable in java?
In a program, initializing an array of 100 KB is throwing an out of memory exception while there is 100 MB of memory available. Why?
What is an object’s lock and which object’s have locks?
What is Gang of four design patterns
Explain how to force the garbage collection in java.
Explain the selection sort algorithm?
What is ‘is-a ‘ relationship in java?
What loop means?
What is the platform?