Because I can call a method without using objects of this even though it is not static

2

very good community my question is the following. I would like to know why I can call a method of a superclass in my subclass without creating an object of this type and in addition to not being static.

is what happens below, I use an inherited method in the subclass constructor but the way that method is called and how it works inside the constructor causes me some anxiety. If someone knows the answer, it would help me a lot.

thanks in advance.

class ClasePrueba{

public void MetodoProbando(){
    int x = 5;
    int y = 5;
    int resultado = x + y;
    System.out.println(resultado);
 }


class ClaseDos extends ClasePrueba{

 public ClaseDos(){
    MetodoProbando();
 }
}

}
    
asked by Teuddy R 22.12.2017 в 04:34
source

1 answer

6

Extends is used to inherit from a class.

When one class inherits another, it is as if you embed the code of the class from which you inherit in your same class.

In your example the ClaseDos inherits from the ClasePrueba . That means, that your ClaseDos will have the same methods and properties of the ClasePrueba (with some restrictions depending on the visibility of the methods and properties, for example the private methods will not inherit them).

In your example, you do not need to instantiate ClasePrueba , since ClaseDos is a sub class of that class, and therefore inherits its properties and public (and protected) methods.

Inheritance definition: here

    
answered by 22.12.2017 / 05:16
source