Downcasting is considering an object to be an instantiation of a subclass, i.e.
farther away from the root. Upcasting is considering an object an instantiation
of a parent class, i.e. more towards the root class (which is the Object class).
Here is an example:
[code=java]
class Base { }
clase Derived extends Base { }
...
Base b= new Derived();
Derived d= new Derived);
Base c= new Base();
...
Derived d= (Derived)b; // ok downcast
Derived e= (Derived)c; // fail downcast
Base f= (Base)d; // always ok upcast
Base g= (Base)c; // always ok useless upcast
[/code]
Downcasting is considering an object to be an instantiation of a subclass, i.e.
farther away from the root. Upcasting is considering an object an instantiation
of a parent class, i.e. more towards the root class (which is the Object class).
Here is an example:
[code=java]
class Base { }
clase Derived extends Base { }
...
Base b= new Derived();
Derived d= new Derived);
Base c= new Base();
...
Derived d= (Derived)b; // ok downcast
Derived e= (Derived)c; // fail downcast
Base f= (Base)d; // always ok upcast
Base g= (Base)c; // always ok useless upcast
[/code]
kind regards,
Jos
This is new to me...
Jos, about line number 5, did you forgot the left parenthesis? or it is an example of fail downcasting?!!
Downcasting is considering an object to be an instantiation of a subclass, i.e.
farther away from the root. Upcasting is considering an object an instantiation
of a parent class, i.e. more towards the root class (which is the Object class).
Here is an example:
[code=java]
class Base { }
clase Derived extends Base { }
...
Base b= new Derived();
Derived d= new Derived);
Base c= new Base();
...
Derived d= (Derived)b; // ok downcast
Derived e= (Derived)c; // fail downcast
Base f= (Base)d; // always ok upcast
Base g= (Base)c; // always ok useless upcast
[/code]
kind regards,
Jos
Hi Jos, Thanks for the reply..
Can the same be done for interfaces. For example i have a Base Interface whose some methods the first class should implement and other methods the second class must implement.
i.e.; all the interface methods should not be available to both the classes.
Only access to particular methods should be given.
Can this be done?? (is this also downcasting??
(Sorry for delay in reply.. Was not able to check net due to some issue)
Can the same be done for interfaces. For example i have a Base Interface whose some methods the first class should implement and other methods the second class must implement.
i.e.; all the interface methods should not be available to both the classes.
Only access to particular methods should be given.
Can this be done?? (is this also downcasting??
(Sorry for delay in reply.. Was not able to check net due to some issue)
I don't understand your question? Are both of these classes abstract (they don't
implement all of the interface methods)? Or is one a subclass of the (abstract)
parent class? But yes, up and down casting works the same for interfaces.
I don't understand your question? Are both of these classes abstract (they don't
implement all of the interface methods)? Or is one a subclass of the (abstract)
parent class? But yes, up and down casting works the same for interfaces.
kind regards,
Jos
No both are concrete classes only.. And there is no inheritance relationship between the classes. Is it possible to implement only some methods of our interface without using abstract concept??
Dont we do that with adaptors in JFC... Is there any technique like that for POJO classes??
No both are concrete classes only.. And there is no inheritance relationship between the classes. Is it possible to implement only some methods of our interface without using abstract concept??
Dont we do that with adaptors in JFC... Is there any technique like that for POJO classes??
If a class states that it implements an interface and it does not implement all
the methods declared in the interface then the class must be declared abstract.
Adaptors always implement all the methods from an interface; they're dummy
implementations . A subclass thereof needs only override those methods that
are needed for that class. For examples see the adapators in the JSE.
If a class states that it implements an interface and it does not implement all
the methods declared in the interface then the class must be declared abstract.
Adaptors always implement all the methods from an interface; they're dummy
implementations . A subclass thereof needs only override those methods that
are needed for that class. For examples see the adapators in the JSE.
kind regards,
Jos
oh k.. thnks jos... I went to an interview.. There they said that, interface can be partially implemented and the concept for that is know asdowncasting..
oh k.. thnks jos... I went to an interview.. There they said that, interface can be partially implemented and the concept for that is know asdowncasting..
Huh? If you only partially implement an interface then that class is abstract by
definition; it has nothing to do with casting in any direction except towards a
loony bin ;-)
Comment