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


Please Help Members By Posting Answers For Below Questions

What does the “final” keyword mean in front of a variable? A method? A class?

639


Write a program to print fibonacci series up to count 10.

590


What is a priority queue java?

601


What is bom encoding?

655


What makes a function well defined?

640






What is function and method in java?

618


Why do we need singleton?

622


What is an array in java?

735


How to Sort Strings which are given in List and display in ascending order without using java api.

3930


What is the finalize method do?

697


Which number is denoted by leading zero in java?

905


What is jdbc api?

633


What are variable arguments or varargs?

670


Is string passed by reference in java?

652


What is method overloading in JAVA? Why is it not present in C ?

666