• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

casting

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ps see the following code:
class base1
{
base1()
{
System.out.println("cons in base ");

}
base1(String s)// this is not accessible in the other classes
{
System.out.println("single parametre cons in base ");
}

}


class constructor extends base1
{
constructor()
{
System.out.println("default of base");
}
constructor(String i)
{
System.out.println("single parameter in base"+ " " + i);
}

public static void main(String[] str)
{

base1 b1 = new constructor("apple"); //1
base1 b2 = new base1("apple"); //2

System.out.println("b1.equals.b2"+b1.equals(b2)); //3
b1=b2; //4
System.out.println("b1.equals.b2"+b1.equals(b2)); //5

}
}


Q) at line 1 b1 is object of constructor class ( child of base1) now at 4
it should give error b'coz casting of parent into child but it ghives no erroe
now at 1 when I write constructor b1 = new constructor("apple");
now it gives erroe asking for explicit casting needed why is this so???

 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
base1 b1 = new constructor("apple"); //1
base1 b2 = new base1("apple"); //2
System.out.println("b1.equals.b2"+b1.equals(b2)); //3
b1=b2; //4
System.out.println("b1.equals.b2"+b1.equals(b2)); //5
Q) at line 1 b1 is object of constructor class ( child of base1) now at 4. It should give error b'coz casting of parent into child but it ghives no error.
***
There is no casting involved here. You have told the compiler that b1 and b2 can hold base1 objects. So the compiler believes you. It assumes that there is nothing wrong with assigning a value from a base1 pointer to another base1 pointer. And in fact there is nothing wrong since you can only run base1 methods on base1 objects.
***
now at 1 when I write constructor b1 = new constructor("apple");
now it gives erroe asking for explicit casting needed why is this so???
***
Now you are trying to use a constructor pointer to hold a base1 object. This won't work. Why? Because you can run constructor methods against the base1 object if it is held by a constructor object. Since the constructor class is a child of base1, the constructor class might have methods that are not in base1.
Example:
public class A {
public void method1() {}
}
public class B extends A {
public void method2() {}
}
B b = new A(); // won't compile
b.method2();
If it was allowed you would get a runtime error for no such method!
A parent can hold their children but children can't hold their parents. Just like in real life!
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


base1 b1 = new constructor("apple"); //1
base1 b2 = new base1("apple"); //2
System.out.println("b1.equals.b2"+b1.equals(b2)); //3
b1=b2; //4
System.out.println("b1.equals.b2"+b1.equals(b2)); //5


During conversion and casting the compile-time data type of variable is considered, not the run-time data type.
when you say, base1 b1 = new constructor("apple");
Compile time data type of b1 is base1 not constructor.
so, at line 4 you can say b1 = b2; (no cating)
But, when you say,
constructor b1 = new constructor("apple");
Now, compile-time data type of b1 is constructor.
then for b1 = b2; compiler shouts and ask for casting.
b1 = (constructor) b2; , This will compile.
B'cos, casting rule is that new type must be super class of old type or VICE-VERSA.
But surely, it will give runtime error, ClassCastException
b'cos b2 (obj of base1) can't assign to b1( of type constructor)
Casting rules are more loose than conversion rule.

Hope this clarifies.
Regards,
Sujit
 
reply
    Bookmark Topic Watch Topic
  • New Topic