ria
ria

Reputation: 7984

method overriding in Java

How is method overriding implemented in Java? In C++ we have the concept of vtable.. how is this implemented internally in Java?

Upvotes: 4

Views: 6449

Answers (6)

JegsVala
JegsVala

Reputation: 1857

Method overriding It means one method is available in supper class which is displaying some string, but you want to extend this class, and also you want to print your own method at that time you need to overriding this method into your local class.
This is a basic introduction to the method overriding.

Example:

class Animal
{
  public void displayMessage()
  {
    System.out.println("Animal is eating");
  }
  
}
class Dog extends Animal
{
  public void displayMessage()
  {
    System.out.println("Dog is eating");
  }
  public static void main(String arg[])
  {
    Dog d=new Dog();
    d.displayMessage();
  }
}

OUTPUT:

Dog is eating

Advantages of Method Overriding is that the class ca give its own specific implementation to an inherited method without any modifying the parent class method.

The rules of Method overriding are:

  • The argument list : the argument list must be the same.

  • Access Modifier : if you're overriding the method you must give the same modifier of super class method suppose super class have the public method you cannot give the protected or private vice versa.

Upvotes: 0

Badri Paudel
Badri Paudel

Reputation: 1630

--Defining the same method with the same method signature(i.e. same number/type of arguments and same return type/s.)in base and derived class.
--Which method is to be called is decided  at runtime so, it is also called runtime polymorphism/late binding. 
--we should override the method defined in the superclass.
--when the method is called the method defined in the subclass is called and executed instead of the one in superclass.
--we overcome this by use of 'super' keyword.
//program
class A
{
void disp(){
System.out.println("class A");
}

}
class B extends A
{
public void disp(){
System.out.println("class B");

}
}
public class ExampleIdea{
public static void main(String[] args){
A a  = new B(); //Parent reference but B class object (we can achieve polymorphism when parent class reference is used to refer a child class object) 
B b = new B();    //B class reference and B class object
A c = new A();
a.disp(); //runs the method in B class 
b.disp();  //runs the method in B class
c.disp();  //runs then method in A class
}
}//end of program

when we run this output will be class B.

In order to access the function of class A we have to use super keyword
as:

 class B extends A
{
public void disp(){
System.out.println("class B");
super.disp();
}

Upvotes: 1

Z.I.J
Z.I.J

Reputation: 1147

Method overriding in Java is a concept based on polymorphism OOPS concept which allows programmer to create two methods with same name and method signature on interface and its various implementation and actual method is called at runtime depending upon type of object at runtime. Method overriding allows you to write flexible and extensible code in Java because you can introduce new functionality with minimal code change.

There are few rules which needs to be followed while overriding any method in Java, failure to follow these rules result in compile time error in Java.

  1. First and most important rule regarding method overriding in Java is that you can only override method in sub class. You can not override method in same class.
  2. Second important rule of method overriding in Java that name and signature of method must be same in Super class and Sub class or in interface and its implementation.
  3. Third rule to override method in Java is that overriding method can not reduce accessibility of overridden method in Java. For example if overridden method is public than overriding method can not be protected, private or package-private; But opposite is true overriding method can increase accessibility of method in Java, i.e. if overridden method is protected than overriding method can be protected or public.
  4. Another worth noting rule of method overriding in Java is that overriding method can not throw checked Exception which is higher in hierarchy than overridden method. Which means if overridden method throws IOException than overriding method can not throw java.lang.Exception in its throws clause because java.lang.Exception comes higher than IOException in Exception hierarchy. This rule doesn't apply to RuntimeException in Java, which is not even need to be declared in throws clause in Java.
  5. You can not override private, static and final method in Java. private and static method are bonded during compile time using static binding in Java and doesn't resolve during runtime. overriding final method in Java is compile time error. Though private and static method can be hidden if you declare another method with same and signature in sub class.
  6. Overridden method is called using dynamic binding in Java at runtime based upon type of Object.
  7. If you are extending abstract class or implementing interface than you need to override all abstract method unless your class is not abstract. abstract method can only be used by using method overriding.
  8. Always use @Override annotation while overriding method in Java. Though this is not rule but its one of the best Java coding practice to follow. From Java 6 you can use @Override annotation on method inherited from interface as well.

Upvotes: 1

Brian Agnew
Brian Agnew

Reputation: 272377

To answer the question, which is specifically how overriding is implemented in the virtual machine, there's a write up available in Programming for the Java Virtual Machine (Google Books link).

The VM will look for an appropriate method definition in the referenced class, and then work its way up through the inheritance stack. Obviously at some stage various optimisations will apply.

See here for a description of the relevant bytecode instruction invokevirtual:

invokevirtual looks at the descriptor given in , and determines how many arguments the method takes (this may be zero). It pops these arguments off the operand stack. Next it pops objectref off the stack. objectref is a reference to the object whose method is being called. invokevirtual retrieves the Java class for objectref, and searches the list of methods defined by that class and then its superclasses, looking for a method called methodname, whose descriptor is descriptor.

As gustafc has highlighted below, various optimisations can apply, and no doubt the JIT will introduce further.

Upvotes: 14

Reuben Peeris
Reuben Peeris

Reputation: 541

Maybe this comparison of C++ Vtables and Java method invocation tables is of interest.

Upvotes: 0

seb
seb

Reputation: 1810

As long as the function (method) you wish to override is not marked as final you just simply override the method by extending the class which holds that method and then provide a method with same signature but different body.

Upvotes: -1

Related Questions