Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

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");
}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic