Abstract classes

1

Whenever we want to overwrite a method of an abstract class from a subclass, this method must be abstract or can be overwritten on a normal method of an abstract class. Thanks.

    
asked by pepinillo 02.12.2016 в 21:06
source

3 answers

2

General rule

Generally, we can overwrite a method that is not declared as static or final in the ancestor class.

Is it possible to overwrite only the abstract methods of an abstract class?

To answer specifically to your question: to overwrite a method, this does not necessarily have to be declared as abstract in the super-class.

Back to the general

Generalizing a bit in java, when you write a class that inherits from another, the methods of an ancestor class can be overwritten (if they are instance methods) and hidden > (if they are methods of the class, also called static).

When you write a class from which others will inherit, you can prevent a method from being overwritten final (it can not be hidden by a descendant). You can also declare some method like abstract , which means that at this point in the hierarchy, its implementation is completely unknown. The class that contains an abstract method must be declared, in turn, as abstract.

When you inherit from an abstract class, you may or may not overwrite all the abstract methods, but if you do not, you must declare the sub-class in turn abstract. You can not create instances of an abstract class.

To finish, I want to delve a bit into the part of

hide and overwrite.

When in a class you declare a method as static, the sub-classes can not overwrite it, only hide it.

There is an important difference between overwriting and hiding. When you overwrite a method, for the outside world it is as if the method of the ancestor class ceases to exist completely, the new method will always be called, regardless of the type of variable on which the invocation is made.

On the other hand, when you hide a static method, both continue to exist for the outside world, and the method that will be called in run time will be determined according to the visibility of the class (or the type of the variable) on which it is invoked.

Let's see an example, adapted from the official documentation about overwriting and hiding Suppose we have two classes:

public class Animal {
    public static void testClassMethod() {
        System.out.println("Método estático en Animal");
    }
    public void testInstanceMethod() {
        System.out.println("Método de instancia en Animal");
    }
}

The second class, which is an animal sub-class, is called Cat :

public class Cat extends Animal {
    public static void testClassMethod() {
        System.out.println("Método estático en Cat");
    }
    public void testInstanceMethod() {
        System.out.println("Método de instancia en Cat");
    }

    public static void main(String[] args) {
        Cat myCat = new Cat();
        Animal myAnimal = myCat;
        Animal.testClassMethod();
        myAnimal.testInstanceMethod();
    }
}

The Cat class overwrites the declared instance method in Animal and hides the static method declared in Animal . The main method in this class creates an instance of Cat and invokes the method testClassMethod() in the class and testInstanceMethod() in the instance.

The output of the program is as follows:

Método estático en Animal
Método de instancia en Cat
    
answered by 02.12.2016 в 21:29
0

You can over a "normal" as you say, but it is not mandatory. The abstractors must be implemented obligatorily .

EDIT: Unless it is also abstract as it says @ SJuan76. I had understood that was not the case. You can learn more by reading the documentation about it .

    
answered by 02.12.2016 в 21:20
0

Requirements for overwriting methods

  • An instance method can only be overwritten if the subclass inherits them (method public ). If a method can not be inherited (method private ), it can not be overwritten.
  • A declared method final can not be overwritten. A private method is implicitly final .
  • A declared method static can not be overwritten, but can be re-declared.
  • A constructor method can not be overwritten.

Consequently.

  

Should a method of an abstract class necessarily be abstract?

Not necessarily.

  

Can you overwrite a normal method of an abstract class?

Yes. As long as it is not final or private .

    
answered by 17.08.2018 в 20:45