• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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: 43028
76
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic