what is object type casting? give some example with related?

Answers were Sorted based on User's Feedback



what is object type casting? give some example with related?..

Answer / 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

what is object type casting? give some example with related?..

Answer / aaratim

Object type casting is converting a object type into another
type. For example

Iterator ite = emplist.iterator();
while(ite.hasNext()){

Employee empObj = (Employee)ite.next();

}

Here ite.next returns instance of type Object, we need to
convert it to type Employee so this is called type casting.

Is This Answer Correct ?    5 Yes 0 No

Post New Answer

More Core Java Interview Questions

What is charat java?

0 Answers  


Can an interface have a class?

0 Answers  


Draw a UML class diagram for the code fragment given below: public class StringApplet extends Applet { private Label sampleString; private Button showTheString; private ButtonHandler bHandler; private FlowLayout layout; public StringApplet() { sampleString = new Label(" "); showTheString = new Button (" Show the String"); bHandler = new ButtonHandler(); layout = new FlowLayout(); showTheString.addActionListener(bHandler); setLayout(layout); add(sampleString); add(showTheString); } class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { samplestring.setText("Good Morning"); } } } Note: The methods need not be indicated on the diagram.

0 Answers  


How to Create A Wapper Class in core Java and Why are Use in java?

2 Answers   Amdocs,


What is difference between an object and a class?

0 Answers   Amdocs,


How do you compare two objects?

0 Answers  


What part of memory - Stack or Heap - is cleaned in the garbage collection process?

1 Answers  


What is the difference between arraylist and hashset in java?

0 Answers  


Can we use String with switch case?

0 Answers  


What does exclamation mean in java?

0 Answers  


Difference between Superclass and Subclass?

7 Answers  


What is the default size of set in java?

0 Answers  


Categories