Inheritance and manipulation of a superclass’s methods

Asked

Viewed 335 times

1

Inheritance is said to inherit all the methods of a super class. But in 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 is an error because the subclass cannot directly manipulate the superclass methods. So how would it be?

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

2 answers

2

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

What you want is to use class Summing up, for this it is necessary to create an instance of it with the operator new, as you did when you needed to use the class Scanner.

The code will be 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 operator extends) the capabilities of a class, keeping those of the inherited class.

Suppose you want a class that does sums and subtractions. Since you already have a class that does sums creates a new one that extends hers:

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 Calculations, has access not only to the methods stated therein, but also to those of the class Summing up:

Calculos calculos = new Calculos();

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

calculos.setSubtracao(20.5,10.5);
double subtracao = calculos.getSubtracao();

2

You shouldn’t use inheritance like this. In fact it is inheritance is something bad and should only be used as a last resort, when it proves more beneficial than the difficulties that its implementation brings. It’s always better to use composition.

But I understand that you’re just trying to take a test with inheritance.

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

I also separated instantiation from execution. So it is more organized even if this is not necessary to work. Note that in the method executa() I could normally call any member, even inherited ones because this method is instance and not static. Ramaral gave an example in his reply without having to create an extra method but uses the same solution to instantiate the class to access the members.

That’s 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());
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.