Inheritance and manipulation of methods of a superclass

1

Inheritance is said that a subclass inherits all methods from a superclass. But when doing

class Soma{
    private double total;
    public void setSoma(double s1, double s2)
    {
        total = s1+s2;
    }
    public double getSoma()
    {
        return total;
    }
}
import java.util.Scanner;

class Calculos extends Soma{
    public static void main(String[] args)
    {
        double v1, v2;
        Scanner insere = new Scanner(System.in);
        System.out.print("Digite o primeiro valor: ");
        v1 = insere.nextDouble();
        System.out.print("Digite o segundo valor: ");
        v2 = insere.nextDouble();
        setSoma(v1, v2);
        System.out.printf("O resultado da soma é %.2f", getSoma());
    }
}

It gives error because the subclass can not manipulate the methods of the superclass directly. So what would it be like?

    
asked by anonymous 28.02.2015 / 13:55

2 answers

2

Do not use inheritance in this way. In fact, inheritance is a bad thing and should only be used in the latter case when it proves more beneficial than the difficulties that its implementation brings. It's always better to use makeup.

But I understand you're just trying to test for inheritance.

There are some ways to do this but you can not really access instance methods in the static method directly. You can only do this by instantiating the class itself. There you can access any member through the instantiated object.

I also separated the instantiation of the execution. This makes it more organized even if it is not necessary to function. Note that in the executa() method I could normally call any member, even those inherited because this method is instance and not static. The ramaral gave an example in his response without having to create an extra method but uses the same solution to instantiate the class to access the members.

How it works:

import java.util.Scanner;

public class Calculos extends Soma {
    public static void main(String[] args) {
        Calculos calculo = new Calculos();
        calculo.executa();
    }
    public void executa() {
        double v1, v2;
        Scanner insere = new Scanner(System.in);
        System.out.print("Digite o primeiro valor: ");
        v1 = insere.nextDouble();
        System.out.print("Digite o segundo valor: ");
        v2 = insere.nextDouble();
        setSoma(v1, v2);
        System.out.printf("O resultado da soma eh %.2f", getSoma());
    }
}

See running on CodingGround .

    
28.02.2015 / 14:19
2

What I think you intend, in this case, has nothing to do with inheritance.

What you want is to use the Sum class, so you need to create an instance of it with the new operator, just like you did when you needed to use the / em>.

The code will look like this:

public static void main(String[] args)
{
    double v1, v2;
    Scanner insere = new Scanner(System.in);
    Soma soma = new Soma();

    System.out.print("Digite o primeiro valor: ");
    v1 = insere.nextDouble();
    System.out.print("Digite o segundo valor: ");
    v2 = insere.nextDouble();
    soma.setSoma(v1, v2);
    System.out.printf("O resultado da soma é %.2f", soma.getSoma());
}

Inheritance is something else, it is used when you want to extend (hence Java using the extends operator) capabilities of a class, keeping those of the class inherited.

Suppose you want a class that adds and subtracts. Since you already have a class that makes sums it creates a new one that extends it:

class Calculos extends Soma{

    private double total;
    public void setSubtracao(double s1, double s2)
    {
        total = s1-s2;
    }
    public double getSubtracao()
    {
        return total;
    }
}

Now, the class class has access not only to the methods declared in it, but also to those of the sum class:

Calculos calculos = new Calculos();

calculos.setSoma(10.5,15.5)
double soma = calculos.getSoma();

calculos.setSubtracao(20.5,10.5);
double subtracao = calculos.getSubtracao();
    
28.02.2015 / 14:18