• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Please help Big Confusion

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help me out here guys...

Then I do this inside "sub" class :

base B_class = new sub(); // <<----- sub class object type.
sub S_class = new sub();

base.method_1(); // This runs method_1() in "sub" class !!!

System.out.print(base.x); // This prints 1. The base class "x" value!!!

base.method_not_in_base(); // This gives compiler error saying no such method !!!

Why is that I can access overriden methods in "sub" class but not variables?

how can I invoke a overried method in "sub" class from "base" instance, but not "method_not_in_base" which is a new method?

Please help guys.... "-(
[ May 31, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


Why is that I can access overriden methods in "sub" class but not variables?




Coming to the answer...

base B_class = new sub();

Here you are creating instance of sub class & assigning it to the base class reference variable.

So just imagine whatall things are there in this instance ( new sub(); ).

- int x=2;

- public void method_1 () { ..... }

- public void method_not_in_base() {.....}

- few other implicit methods (Ignore this point at the moment).


thats all we have in this instance.
Now You must be thinking

what happened to int x = 1 of base?
Ans:
variables are not inherited in java.Only methods are inherited.
Here in this instance you have overriden public void method_1 ()
which was inherited from base class.

why does it show error when we invoke method_not_in_base() {.....}?
Ans:
Though your instance has got method_not_in_base() but still you can
not access it,you know why because you are invoking it on the reference
variable of base class.
base B_class = new sub();

Here B_class is your reference variable of base class & new sub() is the instance of sub class.In other words your base class doesn't know
about method_not_in_base() because it is defined in the sub class.
However method_1(); can be invoked because base class knows that it has got method_1() method in it but since that method has been overriden in the sub class it will invoke it from sub class.


I hope this may help You.
If u have any queries or doubts you can ask...

One more thing when you post some code then try to maintain coding standard.
bkz Your code is quite confusing...

Thanks & Regards.
Waez.
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Waez Ali.. That was a great explanation !! Thanks.. I totally understood it.. Thanks a lot man..

One more question. When Java allocate memory, it also assigns the type of object associated with that memory location.

So, if I say;

base B1 = new Sub();

Does that memory location has "sub" type or "base" type?

Thanks..
 
Waez Ali
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Maduranga,

base B1 = new Sub();
Does that memory location has "sub" type or "base" type?



Coming to the answer that instance type is of Sub class
because instace is created when you say new Sub().However it is allowed in java to assign this instance to the reference variable of base class.But it doesn't mean that you can access each & every thing of this instance by using reference variable of the base class,you can access only those things that base class knows.
Thats the problem you were facing.
I know its bit confusing but it becomes very easy when u do some practise.
Do one thing when you extend some class from other class then try to find out

1 - what would be contents of instance
2 - what all things are coming from parent class
3 - which methods are getting overriden
4 - which methods are getting overloaded.

Try it on your own its really easy.

Thanks & Regards
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
example

O/p will be

Welcome printed from Derived
Value of i 1
Value of i 12
[ May 31, 2005: Message edited by: Barry Gaunt ]
 
shiva rao
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forgot to mention in the above code commented line give me compilation error.

// Derived base = (Derived)(new Base());

Cannot I cast and store the base class in the Derived class.

Thanks

shiva
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic