Instance MethodsAn 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 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 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 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 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 ( 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 public class FlyingCar implements OperateCar, FlyCar { // ... public int startEngine(EncryptedKey key) { FlyCar.super.startEngine(key); OperateCar.super.startEngine(key); } } The name preceding 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 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. Defining a Method with the Same Signature as a Superclass's Method
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. When a subclass method has the same name and argument types as a superclass method?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. In that case, the method in the subclass overrides the method in the superclass and effectively replaces its implementation, as shown in Figure 6-3.
Which of the following statements about super classes and sub classes are true?- the class Class is the super class of all other classes in Java. A super class is a sub set of sub class because a sub class will have some (no final methods) methods of super class and can have its own methods and variables.
Which keyword is used to create a subclass?Explanation: SUPER keyword is used to create a subclass in the class definition.
Which reserved word must be used to call a method of a superclass?The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor.
|