doubt with instance of objects in java

1

I have a question about instantiating objects in Java. If I have two classes and one extends to the other, when I make an instance of any of them with:

A c = new B();

what I do with this is instantiate class B but with the attributes of class A?

For example, with this code:

A.java

public class A {
    public void printValue(){
        System.out.println("A");
    }
}

B.java

public class B extends A {
    public void printValue(){
        System.out.println("B");
    }
}

Test.java

public class Test {
    public static void main(String... args) {
        A b = new B();
        b.printValue();
    }
}

should I receive String A as output, since with A b = new B () I am instantiating B with the attributes of class A? However I receive "B".

Another example:

A.java

public class A {
public static void value(String y) {
System.out.println("A");
}
public static void main(String[] args) {
A c = new B();
String x = "B";
c.value(x);
}
}

B.java

public class B extends A{
    public static void value(String x) {
        System.out.println("B");
    }
}

in this example the opposite happens. Instance class B with A c = new B () stop I always receive as output "A" which is the attribute of class A

    
asked by virtual8870 24.01.2018 в 18:50
source

3 answers

2

Greetings, virtual8870.

  

What I do with this is instantiate class B but with the attributes   of class A?

Something like that, when you extend one class from another, the class that you create (in this case B ) is a class daughter , which will be a new object that it will be similar to the parent class (in this case A ). He will not only have the attributes of his father, but also his methods.

  

should receive String A as output, since with A b = new B ()   I'm instantiating B with the attributes of class A? But nevertheless   receipt "B".

No, this behavior is fine. The reason why you do not receive "A" is because you are overwriting the printValue() method (that B inherited from A ).

In Java , overwriting a method that comes from a parent class is known as @Override . In Spanish, this word means something like 'annul' or 'overcome', and that is precisely what you are doing. You are canceling the original behavior for a new one and the new behavior you gave it was:

System.out.println("B");

That's why you get "B" as a result.

To get the result you expect, you must use super.printValue();

This allows you to use or call the original method that you inherit from your father, without overriding or modifying your result.

So in summary, class 'B' should look like this:

public class B extends A {

    @Override
    public void printValue() {
        super.printValue();
    }
}
    
answered by 25.01.2018 / 01:10
source
1

What you're grabbing is called polymorphism --- > substitution principle:

We can remove an object from the request if the program expects an object from the superclass. But to understand this and to be clear about what is the Heritage and other basic concepts of the OOP

When an intansia class (a extends b) to be assigned to the class that inherits the most, that is, subclass b and the superclass, the subclass inherits all its constructor parameters that class b and others can have. And also a static method can only act on static variables or class variables that are not copied by each intance or object but has a unique copy for each of these intancias for this reason a static method can only be invoked by putting the name of the class in front of said method because it belongs to the class in which it was declared no to any object. Besides that and to respect the encapsulation for this when declaring an object attribute must be put private in front of said variable private animal int intimal ;. Also when you inherit the rule must be applied is a question is whether a subclose is a superclass that is is not appropriate should be from a main class as Animal and that Inherited Dog, Cat and Monkey can be inferred that a Dog is an Animal a Cat is an Animal And a Monkey is an animal without any, it shares the same characteristics to give a different characteristic to each class the abstr methods are used acts or abstract classes since when declaring a single abstract method the class in which it is declared has to be abstract as well as the method that subtly differs from the interfaces that hold almost the same but have a characteristic that they are different and that they can use as a multiple inheritance since one Interfas can inherit from another and one class implement an interface and also inherit the methods of the two interfaces plus the methods and other things that may contain the other superclasses.

    
answered by 03.05.2018 в 03:07
0

In the first example, you have instance methods. In the second are class methods (static).

For the first, the type of the declared variable does not matter since at run time the executed method is the instantiated one.

For the second only the class declared is important since the method is class.

    
answered by 24.01.2018 в 18:59