In an object-oriented programming language, return type covariance means that a method’s return type can be replaced with a narrower one when overridden in a subclass or child class. It eliminates the need to cast or validate the return type when limiting the return type of a custom method. Show
Only non-primitive return types can use a covariant return type. Subclass methods can override base class methods by altering the return type of the overriding method. The term ‘method overloading’ refers to changing a method’s arguments, which the article will discuss further. Table of contents
The covariant return typeThe return statement returns control to the method’s caller. Every Java method must have a return type stated before being used. A primitive type like int, float, reference, or void type can be the return value. As we said earlier, covariant return type means that a method’s return type can be replaced with a narrower one when overridden in a subclass or child class. Here we will look deeper into how we can override methods using covariant return types. Rules regarding covariant return typeFor covariant return types, there are primarily three rules to keep in mind. Following is a list of them:
To demonstrate covariant return type, let’s look at how we can override a method by adjusting its return type. //base class class Color{ // method getcolor returns an object of type color public Color getColor(){ Color s = new Color(); return s; } } //this class will inherit from the class color and will override the getcolor() method returning the object of the type red class class Red extends Color{ @Override public Red getColor(){ Red s = new Red(); return s; } } The application above fails when the Base and Derived return types are swapped. @override annotationMethod overriding is declaring a method in a subclass that is already existent in the parent class. Overriding is used to allow a child class to override a parent class’s implementation of a method. Overriding a method with an exception will require you to follow these three crucial guidelines while using Java. These are a few of them:
Suppose you don’t utilize the @Override annotation. In that case, it won’t cause any problems, meaning that when a superclass method (an overridden method) says it can throw an exception, any overrides must declare that they can throw the same type of exception or a subtype of that exception. It also has some advantages, which we will see below:
Let us now have a look at an example based on @override annotations: class Vehicle { Vehicle getObject() { System.out.println("The Base class method."); return new Vehicle(); } } class Car extends Vehicle { //Overriding getObject method. @Override Car getObject() { System.out.println("Derived class method from Base class."); return new Car(); . } } public class Main { public static void main(String[] args) { Car lexus = new Car(); lexus.getObject(); } } OutputDerived class method from Base class. Because classes are non-primitive return types, covariant types are only conceivable, and by so, you should note this. Advantages of covariant return type
Method overloadingMethod overloading refers to the process of changing the parameters of a method, as indicated earlier. The Java Virtual Machine (JVM) has always supported overloading based on the return type. Because the JVM looks up or resolves a method’s full signature, this is acceptable in the JVM. Multiple methods of the same kind may exist in the same class. Covariant return types are supported and implemented by the java compiler (javac). On the other hand, you can use the Java class file disassembler Javac to verify the code. The covariant return type in Java is made possible thanks to javac’s adoption of this method. To support covariant return types, the JVM does not require any changes. You can’t only change the return type during method overloading. If we merely alter the return type, the compiler will have difficulty determining which function to use. As a result, changing the return type isn’t an option. The following are some advantages of method overloading:
Disassembled Red class codeThis code was generated by javac based on previous examples of Color and Red classes. class Red extends Color { Red(); public Red getColor(); public Color getColor(); } ConclusionBy declaring methods in Java as a child class or derived class and having a name that is the same as the parent class or base class and with the same amount of parameters, the overriding method is said to be invariant in terms of the return type. If the return type is a subclass of the overridden method’s return type, we can override a method by modifying its return value. The primary method can be overloaded in Java, but the JVM only runs the original primary method; it never calls the overloaded main method. Happy learning! Peer Review Contributions by: Briana Nzivu What happens when superclass and subclass has a methods that have same name?If your subclass defines a method with the same name and signature as a method in its superclass, the method in the subclass overrides the one in the superclass. Thus, the subclass does not inherit the method from its superclass.
When a method in a subclass has the same name and type?Explanation: When a method in a subclass has the same name and type signatures as a method in the superclass, then the method in the subclass overrides the method in the superclass.
When a method in a subclass has the same name and type signature in its superclass then the method in the subclass is said as method ?Static Methods
If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.
Can a subclass have a method with the same name?A subclass can do more than that; it can define a method that has exactly the same method signature (name and argument types) as a method in its superclass.
|