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