logo

Java Overriding


Show

In the earlier episode, we conversed concerning superclasses and subclasses. If a class takes over a technique from its superclass, then there is a probability to dominate the technique if it is not spotted final.

The advantage of superseding is: the aptitude to describe a behavior that is precise to the subclass type, which signifies a subclass, can realize a parent class process based on its condition.

In object-oriented words, overriding denotes to supersede the functionality of an obtainable method.

Example

Let us look at an example.

class Animal {
   public void move() {
      System.out.println("Animals can move");
   }
}

class Dog extends Animal {
   public void move() {
      System.out.println("Dogs can walk and run");
   }
}

public class TestDog {

   public static void main(String args[]) {
      Animal a = new Animal();   // Animal reference and object
      Animal b = new Dog();   // Animal reference but Dog object

      a.move();   // runs the method in Animal class
      b.move();   // runs the method in Dog class
   }
}

This will produce the following result:

Output

Animals can move
Dogs can walk and run

In the over instance, you can perceive that still though b is a kind of Animal it jogs the shifting process in the Dog class. The motive for this is: In accumulated time, the confirmation is made on the position type. However, in the runtime, JVM figures out the thing type and would sprint the technique that belongs to that exacting object.

Consequently, in the over instance, the agenda will amass properly while Animal class has the technique shift. Then, at the runtime, it lopes the technique exact for that object.

Consider the following example:

Example

class Animal {
   public void move() {
      System.out.println("Animals can move");
   }
}

class Dog extends Animal {
   public void move() {
      System.out.println("Dogs can walk and run");
   }
   public void bark() {
      System.out.println("Dogs can bark");
   }
}

public class TestDog {

   public static void main(String args[]) {
      Animal a = new Animal();   // Animal reference and object
      Animal b = new Dog();   // Animal reference but Dog object

      a.move();   // runs the method in Animal class
      b.move();   // runs the method in Dog class
      b.bark();
   }
}

This will produce the following result

Output

TestDog.java:26: error: cannot find symbol
      b.bark();
       ^
  symbol:   method bark()
  location: variable b of type Animal
1 error

This plan will fling an accumulated time fault since b's position type Animal never have a technique by the first name of bark.

Rules for Method Overriding

  • The disagreement list should be accurately similar to that of the overridden technique.
  • The revisit kind should be similar or a subtype of the revisit type stated in the unique overridden technique in the superclass.
  • The admission level never is more preventive than the overridden method's entrĂ©e level. For instance: If the superclass process is stated public then the overriding technique in the subclass never be either confidential or protected.
  • Instance ways can be overridden only if they are innate by the subclass.
  • A process stated final never be overridden.
  • A method announced stationary never be overridden except can be re-declared.
  • If a technique never is inborn, then it never is overridden.
  • A subclass in a similar wrap-up as the example's superclass can dominate any superclass technique that is not stated private or final.
  • A subclass in a diverse box-up can only dominate the non-final process announced public or protected.
  • An overriding technique can fling any uncheck omissions, despite whether the override technique throws immunities or not. However, the overriding way should not toss checked exemptions that are new-fangled or broader than the singles announced by the override process. The overriding technique can bowl narrower or fewer exceptions than the overridden method.
  • Constructors cannot be overridden.

Using the super Keyword

When invoking a superclass version of an overridden method the super keyword is used.

Example

Learn how 2 solve the Rubiks Cube with the layer method, learning only six algorithms.

class Animal {
   public void move() {
      System.out.println("Animals can move");
   }
}

class Dog extends Animal {
   public void move() {
      super.move();   // invokes the super class method
      System.out.println("Dogs can walk and run");
   }
}

public class TestDog {

   public static void main(String args[]) {
      Animal b = new Dog();   // Animal reference but Dog object
      b.move();   // runs the method in Dog class
   }
}

This will produce the following result:

Output

Animals can move
Dogs can walk and run

Here at Intellinuts, we have created a complete Java tutorial for Beginners to get started in Java.