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

Does treeset use compareto?

755


Mention the default values of all the elements of an array defined as an instance variable.

748


What is core java used for?

771


What all methods are used to prevent thread execution ?

766


What is a singleton factory?

738


What is fundamental datatype?

743


Does java support function overloading, pointers, structures, unions or linked lists?

851


What is use of super keyword in java?

822


How do you change an int to a string?

766


why are wait(), notify() and notifyall() methods defined in the object class? : Java thread

765


What do you mean by compiler?

824


What is the use of object and class classes?

808


When can you say a graph to be a tree?

854


I want my class to be developed in such a way that no other class (even derived class) can create its objects. How can I do so?

756


Give reasons supporting that string is immutable.

727