• 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

Explain....Inheritance

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Super
{
void method(Super s1)
{
System.out.println("Super");
}
}

class Sub extends Super
{
void method(Sub sb1)
{
System.out.println("Sub");
}
}

class TestInher
{
public static void main(String [] args)
{
Super sp1 = new Super();
Sub sb1 = new Sub();
Super spb = new Sub();

spb.method(sp1); //Produces O/P Super "No Doubt in it"
spb.method(sb1); //Produces O/P Super "Why"
spb.method(spb); //Produces O/P Super "No Doubt in it"
}
}

// I thought that it should print "Sub" for second method call...
// Why it prints "Super".....
// When we call method using super class reference and sub type
// object, the method in the sub class should be called... But, here
// Why the super class method is called... Plz explain.....
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Sub extends Super
{
void method(Sub sb1) //oveloading, no overriding
{
System.out.println("Sub");
}
}


Sub sb1 = new Sub();
Super spb = new Sub();

spb.method(sb1); //spb is Super and Super dont know about overloading method in Sub

If you need overriding


class Sub extends Super
{
void method(Super sb1)
{
System.out.println("Sub");
}
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic