What is the restrictions in castng...
Inthe following prog if i uncomment c = p.
It throws,
Casting.java:28: incompatible types
found : test.Parent
required: test.Child
is it possible to cast both up and down the hierarchy ? -
package test;
-
-
class Parent {
-
public static void print(){
-
System.out.println("Parent");
-
}
-
}
-
-
class Child extends Parent {
-
public static void print()
-
{
-
System.out.print("Child");
-
}
-
}
-
-
class GrandChild extends Child {
-
public static void print()
-
{
-
System.out.print("GrandChild");
-
}
-
}
-
public class Casting {
-
public static void main(String args[]){
-
Parent p = new Parent();
-
Child c = new Child();
-
GrandChild g = new GrandChild();
-
p = c;
-
// c = p;
-
System.out.println("Compiles Good");
-
}
-
}
-
Thanks,
P.Jerald
7 2923
What is the restrictions in castng...
Inthe following prog if i uncomment c = p.
It throws,
Casting.java:28: incompatible types
found : test.Parent
required: test.Child
is it possible to cast both up and down the hierarchy ? -
package test;
-
-
class Parent {
-
public static void print(){
-
System.out.println("Parent");
-
}
-
}
-
-
class Child extends Parent {
-
public static void print()
-
{
-
System.out.print("Child");
-
}
-
}
-
-
class GrandChild extends Child {
-
public static void print()
-
{
-
System.out.print("GrandChild");
-
}
-
}
-
public class Casting {
-
public static void main(String args[]){
-
Parent p = new Parent();
-
Child c = new Child();
-
GrandChild g = new GrandChild();
-
p = c;
-
// c = p;
-
System.out.println("Compiles Good");
-
}
-
}
-
Thanks,
P.Jerald
No you cannot cast both up and down the inheritance hierarchy.
I'm sure you can play around on the compiler to find out which casts are allowed. Also take note of the difference between casts that fail at compile time and those that fail at run time.
No you cannot cast both up and down the inheritance hierarchy.
You can always cast upwards, towards the root which is the Object class. You
can only cast downwards if the object at hand actually *is an* instance of the
target class you want to cast to or a subclass thereof. A Parent most certainly
is not a Child in the example, hence the cast failure.
kind regards,
Jos
You can always cast upwards, towards the root which is the Object class. You
can only cast downwards if the object at hand actually *is an* instance of the
target class you want to cast to or a subclass thereof. A Parent most certainly
is not a Child in the example, hence the cast failure.
kind regards,
Jos
Yep, I suppose my post ended up all wrong. I wanted to convey that it's not correct to say that "it's always possible to cast both ways" and that the OP is best left to discover those that work and those that do not work ...
A Parent most certainly is not a Child in the example, hence the cast failure.
kind regards,
Jos
Thanks, jos and r035198x.
Regards,
P.Jerald.
You can always cast upwards, towards the root which is the Object class. You
can only cast downwards if the object at hand actually *is an* instance of the
target class you want to cast to or a subclass thereof. A Parent most certainly
is not a Child in the example, hence the cast failure.
kind regards,
Jos
So, We cannot cast a parent class to its child. Am i wright ?
So, We cannot cast a parent class to its child. Am i wright ?
Yep, true; here's an example: -
public class MeansOfTransportation { ... }
-
public class CompactCar extends MeansOfTransportation { ... }
-
public class Submarine extends MeansOfTransportation { ... }
-
...
-
Submarine yellow= new Submarine();
-
MeansOfTransportation mot= (MeansOfTransportation)yellow; // valid up-cast
-
CompactCar c= (CompactCar)mot; // invalid down-cast
-
As you can see a submarine clearly isn't a compact car.
kind regards,
Jos
You need an explicit cast from parent class to child class but no need for explicit class from child to parent to child so if you do this there will be no error
#
public static void main(String args[]){
#
Parent p = new Parent();
#
Child c = new Child();
#
GrandChild g = new GrandChild();
#
p = c;
#
c = (Child)p;
#
System.out.println("Compiles Good");
#
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: ofer |
last post by:
Hi,
I am working with the beta version of the new .net framework (Whidbey) and I
encountered a problem with serialization that did'nt exist in the .net 2003
the situation is like this :
I have...
|
by: Vic |
last post by:
Hi,
in VB.NET there is the following code:
Friend htProvidedProperties As New Hashtable()
Private Class TextboxValidatorProvidedProperties
Public DataType As DataTypeConstants
Public...
|
by: Cybertof |
last post by:
Hello,
In the code below, is there a shortcut not to use the temp var
CurrEmployee ?
I would like to use something like
"if (Employee)MyArrayList.Name"
but don't know the syntax....
|
by: Peter |
last post by:
Hello
Thank for reviewing this question. I would like to know why I am getting a syntax error. For example
public class
public class B : public
|
by: ofer |
last post by:
Hi,
I am working with the beta version of the new .net framework (Whidbey) and I
encountered a problem with serialization that did'nt exist in the .net 2003
the situation is like this :
I have...
|
by: Michel |
last post by:
Hi all!
I created a new class, which has the UserControl as a base class.
Now I want to create an instance of my new class, by creating a new
usercontrol and casting it to my new class using...
|
by: Michael |
last post by:
Hi,
I think my problem deals with class casting and inheritance.
I want to deal with various Audio Formats, reading into memory for
modification, working with it (done by different classes),...
|
by: Kris |
last post by:
Hi.
I have a asp class A with a public function AA()
----------------A.aspx.cs------------------------
public partial class A : System.Web.UI.Page
{
protected void Page_Load(object sender,...
|
by: Magnus |
last post by:
i have a class which inherits from a base class
class base1 {
}
class child1 : base1 {
}
how can I determine if child1 is of base1? gettype returns type of child1,
but I need to know the...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |