• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

doubt in inheritance

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am arulraj and have a doubt in inheritance..

please see the following example.

class A
{
int a=10;
}
class B
{
int a=20;
}

A objA=new A();
B objb=new B();

objA=objb; // 1
objb=objA // 2 it says compiler an error.


In the above example , if you assign a subclass object to a superclass object, the program compiles and executes...

if you do reverse operation (assign a superclass object to subclass object), the compiler says an error....

1. Can you pleas explain it why this one is not allowed?

2.
System.out.println("objA.a:"+objA.a);
it prints value 10.
May i know why it gives the value 10 instead of 20?

3.. what is difference between object and object reference?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"arulrajjj michaelrajjj,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names.

You can edit your display name here. Thank you for your prompt attention!

-Marc
 
arulraj michaelraj
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am arulraj and have a doubt in inheritance..

please see the following example.

class A
{
int a=10;
}
class B extends A
{
int a=20;
}

A objA=new A();
B objb=new B();

objA=objb; // 1
objb=objA // 2 it says compiler an error.


In the above example , if you assign a subclass object to a superclass object, the program compiles and executes...

if you do reverse operation (assign a superclass object to subclass object), the compiler says an error....

1. Can you pleas explain it why this one is not allowed?

2.
System.out.println("objA.a:"+objA.a);
it prints value 10.
May i know why it gives the value 10 instead of 20?

3.. what is difference between object and object reference?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inheritance indicates an "is a" relationship. An instance of a subclass is a specialized version of an instance of its superclass. It is easier to see if you use other names than "A" and "B" for your classes. For example:

About question 2: Member variables cannot be overridden just like methods.

About question 3: An object is "the thing itself" and a reference is something that points to the object. In Java, variables are references to objects (unlike for example C++ where variables hold the objects themselves).
[ March 08, 2007: Message edited by: Jesper Young ]
 
arulraj michaelraj
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks jesper....
 
Not looking good. I think this might be the end. Wait! Is that a tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic