• 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

clarification in code

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could anyone pl.clarify my doubts..??

class base4
{
int i=99;
public void amethod()
{
System.out.println("Base method()");
}
base4()
{
amethod();
}
}
public class derived extends base4
{
int i=-1;
public static void main(String args[])
{
derived mine = new derived();
mine.amethod();
System.out.println(mine.i);
}
public void amethod()
{
System.out.println("Derived method");
}
}

The above code when compiled and run outputs
Derived method
Derived method
-1
The method call amethod in the Base class construtor seems to bind with the derived class amethod.
But The following code when compiled and run outputs
parent method2
parent method1
eventhough the object is referring to the child2 object and method1 is overriddden in teh child2 class why
parent method1 is executed instead of child2 method1..Pl clarify..

class Parent
{
private void method1()
{
System.out.println("parent method1");
}
public void method2()
{
System.out.println("parents method2");
method1();
}
}
public class child2 extends Parent
{
public void method1()
{
System.out.println("childs methid");
}
public static void main(String args[])
{
Parent p=new child2();
p.method2();
}
}
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because parent method1() is private it can not be overridden by the child. Therefore when the parent runs method1() it knows that the method1() in the child is not overriding its own method1(). SO it runs its own method1().
 
Kumar Mahesh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Thomas
I got it .The method1 declared in the child class is just a method declared with the same name and signature like the method in parent and its not the the overiding method and so it cannot participate in polymorphism.right???
mahesh

Originally posted by Thomas Paul:
Because parent method1() is private it can not be overridden by the child. Therefore when the parent runs method1() it knows that the method1() in the child is not overriding its own method1(). SO it runs its own method1().


 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

so it cannot participate in polymorphism.right???


Correct
------------------
Moderator of the JDBC Forum
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic