• 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

how to call overridden method

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,ranchers

i hv a doubt regarding overriding......

a method overrides superclass's method

then hw to call the overridden method without using super keyword....

plz help me

cheers

vijay

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The object determines which overriden method to be called. If the object is of type super, its method is called.

Thanks
Chandu
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can create an object of the superclass and call the method referencing that object.

public class Class2{

public void testIt() {

System.out.println("member " + 2);
}

}

public class Class3 extends Class2 {

public void testIt() {
System.out.println("member " + 3);
}
public static void main(String[] args) {
Class2 c2 = new Class2();
c2.testIt();

}

}
 
kotha vijaybabu
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no its not lik dat

i hv code like dis.....



class Animal
{
public void eat()
{
System.out.println("hi in animal");
}

}

class Dog2 extends Animal
{

public void eat()
{
System.out.println("hi in dog2");
}

public static void main(String arg[])
{
Animal a=new Dog2();

a.eat();
}

}
here by creating subclass's obj. how to call superclass's method

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont think so you will be able to call the superclass method which is overridden using the subclass object. Becoz JVM decide at the runtime which method to call based on the object it is referencing so if you have overridden the method in the subclass it will always call the subclass method else it will call superclass method.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This should call the superclass

class Animal
{
public void eat()
{
System.out.println("hi in animal");
}

}

class Dog2 extends Animal
{

public void eat()
{
System.out.println("hi in dog2");
}

public static void main(String arg[])
{
Animal a=new Animal(); //notice you call the Animal object

a.eat();
}

}


In your code you seem to be calling a new Dog2 object where in fact you
probably want to be calling a new Animal object which will call the method in the superclass.

I think this is correct but im open for correction as I am new to this too.

Regards
Dave
[ February 28, 2006: Message edited by: David Kennedy ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic