An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method. Show The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method that it overrides. An overriding method can also return a subtype of the type returned by the overridden method. This subtype is called a covariant return type. When overriding a method, you might want to use the @Override annotation that instructs the compiler that you intend to override a method in the superclass. If, for some reason, the compiler detects that the method does not exist in one of the superclasses, then it will generate an error. For more information on @Override, see Annotations. Static MethodsIf 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. The distinction between hiding a static method and overriding an instance method has important implications:
Consider an example that contains two classes. The first is Animal, which contains one instance method and one static method: public class Animal { public static void testClassMethod() { System.out.println("The static method in Animal"); } public void testInstanceMethod() { System.out.println("The instance method in Animal"); } } The second class, a subclass of Animal, is called Cat: public class Cat extends Animal { public static void testClassMethod() { System.out.println("The static method in Cat"); } public void testInstanceMethod() { System.out.println("The instance method in Cat"); } public static void main(String[] args) { Cat myCat = new Cat(); Animal myAnimal = myCat; Animal.testClassMethod(); myAnimal.testInstanceMethod(); } } The Cat class overrides the instance method in Animal and hides the static method in Animal. The main method in this class creates an instance of Cat and invokes testClassMethod() on the class and testInstanceMethod() on the instance. The output from this program is as follows: The static method in Animal The instance method in Cat As promised, the version of the hidden static method that gets invoked is the one in the superclass, and the version of the overridden instance method that gets invoked is the one in the subclass. Interface MethodsDefault methods and abstract methods in interfaces are inherited like instance methods. However, when the supertypes of a class or interface provide multiple default methods with the same signature, the Java compiler follows inheritance rules to resolve the name conflict. These rules are driven by the following two principles:
If two or more independently defined default methods conflict, or a default method conflicts with an abstract method, then the Java compiler produces a compiler error. You must explicitly override the supertype methods. Consider the example about computer-controlled cars that can now fly. You have two interfaces (OperateCar and FlyCar) that provide default implementations for the same method, (startEngine): public interface OperateCar { // ... default public int startEngine(EncryptedKey key) { // Implementation } } public interface FlyCar { // ... default public int startEngine(EncryptedKey key) { // Implementation } }A class that implements both OperateCar and FlyCar must override the method startEngine. You could invoke any of the of the default implementations with the super keyword. public class FlyingCar implements OperateCar, FlyCar { // ... public int startEngine(EncryptedKey key) { FlyCar.super.startEngine(key); OperateCar.super.startEngine(key); } }The name preceding super (in this example, FlyCar or OperateCar) must refer to a direct superinterface that defines or inherits a default for the invoked method. This form of method invocation is not restricted to differentiating between multiple implemented interfaces that contain default methods with the same signature. You can use the super keyword to invoke a default method in both classes and interfaces. Inherited instance methods from classes can override abstract interface methods. Consider the following interfaces and classes: public interface Mammal { String identifyMyself(); } public class Horse { public String identifyMyself() { return "I am a horse."; } } public class Mustang extends Horse implements Mammal { public static void main(String... args) { Mustang myApp = new Mustang(); System.out.println(myApp.identifyMyself()); } } The method Mustang.identifyMyself returns the string I am a horse. The class Mustang inherits the method identifyMyself from the class Horse, which overrides the abstract method of the same name in the interface Mammal. Note: Static methods in interfaces are never inherited. ModifiersThe access specifier for an overriding method can allow more, but not less, access than the overridden method. For example, a protected instance method in the superclass can be made public, but not private, in the subclass. You will get a compile-time error if you attempt to change an instance method in the superclass to a static method in the subclass, and vice versa. SummaryThe following table summarizes what happens when you define a method with the same signature as a method in a superclass.
Note: In a subclass, you can overload the methods inherited from the superclass. Such overloaded methods neither hide nor override the superclass instance methods—they are new methods, unique to the subclass.
At the time of overloading we generally use override with virtual. When we need to override the base class method into derived class than we use override keyword. The difference between override and new is that override extend the method of base class with new definition but new hides the method of base class. Before understanding the difference between both first we have to understand their use. Concept of New KeywordFor Example Create the following base class public class baseClass { public string ShowHello() { return "Base Class hello"; } } Now create the derived class and inherit the base class into it public class derClass:baseClass { public string ShowHello() { return "Derived Class hello"; } } As you can see in derived class it will not show any error but a following warning:- Figure 1 If you assign new with this method it will stop giving warning. Use new in the following manner:- public class derClass:baseClass { public new string ShowHello() { return "Derived Class hello"; } } Now create the object of derived class and call the method ShowHello() using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { derClass dr = new derClass(); Response.Write(dr.ShowHello()); } } You will see the following output:- Figure 2 Override and VirtualOverride simply extends the functionality into derived class. The method which will be overridden in derived class will be assigned with virtual in base class. For example:- I have created the method in the base class and assign it with virtual in the following manner:- public virtual string ShowWelcome() { return "Show Welcome from base"; } Now I go to the derived class and override this method in the following manner:- public override string ShowWelcome() { return "show welcome from derived"; } Now call this method and checks its output:- public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { derClass dr = new derClass(); Response.Write(dr.ShowWelcome()); } } Figure 3 As its showing the method witch is overridden in derived class. Difference between new and OverrideNow you are thinking that there is not any kind of difference between them till now as both are calling the derived class function. Now create the object of derived class but use the type of base class in the following manner:-
public partial class _Default : System.Web.UI.Page
{ protected void Page_Load(object sender, EventArgs e) { //take type of base class and initalize it with derived class object baseClass bc = new derClass(); //concept of new assign here Response.Write(bc.ShowHello()+" The output of the code as follows:- Figure 4 As you can see that when I show method which used new keyword is showing base class method but when I show the method which used override then its shows the method of derived class. As it shows the difference between new and override that override extends the method in derived class but new only hides the method of base class in derived class. |