• 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

Inheritance

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A{
void m1(A a){
System.out.println("A");
}
}
class B extends A{
void m1(B b){
System.out.println("B");
}
}
class C extends B{
void m1(C c){
System.out.println("C");
}
}
class D extends C{
void m1(D d){
System.out.println("D");
}
public static void main(String []args){
A a1=new A();
B b1=new B();
C c1=new C();
D d1=new D();
d1.m1(a1);
d1.m1(b1);
d1.m1(c1);
}
}

Here d1 is the reference of least sub class D.
Is it possible to refer to the Super classes A,B,C?(d1.m1(a1))
Some times these represents compile time error and some times run time error.
Please explain clearly
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure if I got your question correctly or not:

Is it possible to refer to the Super classes A,B,C?(d1.m1(a1))



This is a perfect example of Overloading and hence calls like d1.m1(a1) are perfectly all right.

Murali...
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's also possible to specify directly which method you want by using the "super." prefix, e.g. in class C you can call B.m1 by "super.m1(c)".
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello sukhavasi vasavi,

I think you would have taken this example from danchisholm's chapter 12 (Overloading). The author had given a very clear explanation in the sample chapter (pdf) itself.

As others said, its perfectly legal and allowed. Any subclass can very well refer to its super class at anytime using the keyword 'super' or even directly (because of Inheritance), but the vice-versa is not true!

Just read the pdf here and you may please ask if you still have the doubt.

HtH.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic