Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Inheritance

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at this example...
class Super{
public Super() //1
{
a();
}
void a() //1
{
method();
}
void method() //3
{
System.out.println("1");
}
}
public class Subclass1 extends Super{
public Subclass1()//4
{
a();
}
public void method() //5
{
System.out.println("2");
}
public static void main(String []args) {
Super c = new Subclass1(); // 4 - 1 - 3
}
}

The output of this prog is 2 2
I thought it should be 1 2
how this works?
Super c = new Subclass1(); // 4 - 1 - 3
after this line control will go to super class constructor. and then method a() at 1 and then call to method at 3. but it does not work that way
pls explain...
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to Programmer Certification Study
Please continue the discussion there. Thanks!
Also, "Ana P" Can you please adjust your displayed name to meet the JavaRanch Naming Policy. You can change it here.
Welcome to the JavaRanch!
[ May 08, 2002: Message edited by: Jessica Sant ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're very close, Ana. The difference is that the method "method" is overridden in the class Subclass1. Therefore, when method() is invoked in Super.a(), dynamic binding is used to determine which method to invoke. As the run-time object is of type Subclass1, the method "method()" from Subclass1 is invoked, rather than the method "method()" from Super.
I hope that helps,
Corey
In addition, you can check out this thread which covers this topic.
[ May 08, 2002: Message edited by: Corey McGlone ]
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic