• 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:

something related to overriding

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


i think I am asking silly silly question but what to do when earlier i studied Completed Ref by Schild it was ok for me but this book(SCJP by sierra), each page is increasing my confusion day by day.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rohit shekhar wrote:
in this code it prints class b, it means we can access subclass method from superclass ref... but in previous case it gives error... 1st prog suggests we cant access subclass methods from superclass but if we will override it then that rule vanish???



I think you are reading too much into it... you are using an "a" reference, which means it is an object that IS-A "a".

In the first example, the a class does not have a disp() method, and in the second case, the a class does.

Henry
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rohit shekhar wrote:



Rohit,
It will be easier to read the code if public static void main is used instead of psvm.
See JavaRanch rules about Post Real Code
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Rohit,

Rohit, use full code snippets rather then the short cuts.

In your first example, if you watch carefully, there is no method like disp() in class A. You define that method in class B.
So you are not really overriding disp() but actually you are trying to invoke a method which is present in class B using reference
of class A, which is not possible, because compiler cannot see anything like method disp() in class A.

There is no overriding in example 1.

Hope this helps,
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic