Java

Method Overriding in Java with Programming Examples

Method Overriding in Java:

Method Overriding in java:- In a class hierarchy, if a method in a subclass has the same name and type signature as a method in its upper class( or superclass, then the method in the subclass should override the method in the upper class ( or superclass. When an overridden method is called from a subclass, it will consistently be Reference to the version of this method that is described by the subclass. The version of the defined method from the upper class is hidden. Consider the following:

output:

method overriding in java

When a show() is called on an object of type B, the version of the show() defined in B is used. That is, the version of the show() in B overrides the version declared in A. To access the superclass version of an overridden method, you can do this by with super. For example, this version of B calls the superclass version of the show() within the version of the subclass. This allows all instance variables to be displayed.

If you replace this version of A with the previous program, the following is displayed

Output:

i and j: 1 2

k: 3

Here super.show() calls the superclass version of show().  method Overriding occurs only when the names and type signatures of the two methods are identical. If this is not the case, the two methods are simply overloaded. Oral use, For example, consider this modified version of the previous example:

Output:

method overriding in java

The version of the show() in B uses a string parameter. This makes its type signature differs from the one in A, which does not accept parameters. Therefore, no override (or name) hidden).



Dynamic method dispatch

While the examples in the previous section demonstrate the mechanics of methods overriding, They don’t show his strength. In fact, if there was nothing more to override than methods a naming convention, then it would be at best an interesting curiosity, but of little real value. However, this is not the case. method Overriding is the basis for one of most methods of Java Powerful concepts: dynamic method dispatch. Dynamic method dispatch is the mechanism This purposes a call to an overridden method at run time, not at compile time. Dynamic method dispatch is important because Java implements the runtime in this way Polymorphism.

Let’s start by repeating an important principle: A reference variable of the upper class can refer to a subclass object. Java utilizes this reality to determine calls to overridden methods at run time. Here I’m showing. When an overridden method is called from a superclass reference, Java The version of this method that you want to run depends on the type of the referenced object until the time of the call. This determination therefore takes place at runtime. If different When reference is made to object types, different versions of an overridden method are called. In other words, it is the type of the referenced object (not the type of the reference variable). This determines which version of an overridden method to run. So if a Superclass contains a method that is overridden by a subclass when different types of objects are referenced by a superclass reference variable, different versions of the method.


 example of dynamic method dispatch:

 

Output:

method overriding in java

This program creates an upper class named A and two subclasses of it, B and C. The subclasses B and C declared in A override callme(). Within the main () method, objects from Type A, B, and C are declared, and a reference of type A is declared called r. The program then assigns a reference to each object type and uses this reference to call it Callme( ). As the output shows, the running version of callme() is determined by the type of the object referenced at the time of the call. If it had been determined by the type of Reference variable, r, you would see three calls to the callme() method of A.

Why overridden methods?

As mentioned above, overridden methods allow Java to support runtime polymorphism. Polymorphism is essential for object-oriented programming for one reason: It enables a general class to specify methods common to all derivatives while allowed subclasses to define the specific implementation of some or all of these methods. Overridden Methods are another way that Java implements the aspect “One interface, multiple methods” polymorphism.

Part of the key to the successful application of polymorphism is the understanding that the Upper and lower classes form a hierarchy that changes from a lower to a larger specialization. When used correctly, the upper class provides all the elements that a subclass can use directly. It also defines the methods that the derived class itself must implement. This allows the subclass provides the flexibility to define its own methods and still force a consistent interface. By combining inheritance with overridden methods, an upper class can define the general Form of the methods used by all subclasses. Dynamic runtime polymorphism is one of the most powerful mechanisms that are object-oriented Design affects the reuse and robustness of code. The ability of the existing code Libraries to call methods on instances of new classes without recompiling them during maintenance A clean abstract surface is an extremely powerful tool.


Applying Method Overriding in Java

Let’s look at a more practical example of overriding methods. The following program creates an upper class called Figure that stores the dimensions of a two-dimensional object. It also defines a method called area () that calculates the area of an object. The program derives from two sub-classes from the figure. As you can see below the first is a rectangle, and the second is a triangle. Each of These subclasses overrides area () so that the area of a rectangle is returned ​ ​and a triangle. respectively.

output:

method overriding in java

This is possible due to the double mechanisms of inheritance and of the transit time polymorphism Define a consistent interface that is used by several different but related object types. In this case, if an object is derived from a figure, its area can be determined by calling area (). The interface to this operation is the same regardless of the type of figure used.



Method Overriding toString ():

From the absolute base class java.lang.Object everyone gets Subclasses inherited a method toString () which is mostly used for debugging

Outputs an object identifier:

The method returns the name of the class, followed by an “@” and a hexadecimal identifier. The GameObject class without its own toString () should test the effect:

ToString () returns something on a GameObject object

cryptic identifier:

So it’s a good idea to have toString () in the subclasses too override. A string identifier should include the name of the class and contain the states of an object. For a room, the one (inherited) name and its own size, this can be done as follows appearance:

And the test looks like this:

Method Overriding Rules in java:

The upper class and subclass must have the same return type, parameter, and method name.

When we declared the method as final and static then it cannot be overridden.

Always abstract method of the upper class is overridden.


Prevent Method Overriding with final keyword:

In the inheritance hierarchy, in some cases, a designer wants to prevent subclasses from overriding a method and using implement new logic. The additional modifier prevents this final on the method declaration. Since method calls always dynamically bound, a caller could unintentionally enter the Land subclass, which final methods avoid. Here is an example: The GameObject saves a name internally in a protected attribute name and only allows access via a setter/getter. The setName (String) method tests whether the name is non-zero and contains at least one character. This method should be final because a subclass could do this Easily bypass access restrictions and even with an overridden setName (String) method the protected variable describes the name that each subclass has access to:

When trying to override the method in a subclass, the compiler reports an error:

We’ll leave the example with a protected variable because Subclasses might want to change the variable, but they don’t via setName (String). For example, the subclasses can be the Variable name to the empty string ”” reset what a call is about setName (String) cannot.


Related Articles:

java class and object

Inheritance In Java And Types Of Inheritance With Examples

Java Packages In Full Detail With Programming Examples

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button