Consider the following code classes.
public class A
{
public A()
{
callCreation();
}
protected void callCreation()
{
System.out.println("A Created!!");
}
}
public class B extends A
{
protected void callCreation()
{
System.out.println("B Created!!");
}
}
public class C extends B
{
protected void callCreation()
{
System.out.println("C Created!!");
}
public static void main(String[] args)
{
A a = new A();
A b = new B();
A c = new C();
}
}
The output of running the class C is given below.
A Created!!
B Created!!
C Created!!
The first output line in the output A Created!! is printed because when the constructor of class A is called, it calls the super class's constructor (java.lang.Object) implicitly before calling the callCreation() method in the class A's constructor. And this will be the case for B and C classes too. In that case when the constructor of B is called the call flow should be typically : B's constructor -> A's Constructor -> java.lang.Object's Constructor -> come back to A's callCreation() method to finish calling A's constructor. If so how is the overridden value printed and not the super class's value is printed? So the question is 'when is an object of a class created exactly? to put it in other words, the object of a class should be created only after the constructor finishes calling/initializing all the elements within itself. If so how can a method be called from a child class and not from the parent class?
The callCreation methods in B and C override the method from A. So when the method is called in the constructor A the implementations in B and C will be run, even though the constructors of B and C have not been executed yet. This is possible because constructors don't actually create the object, rather they are called to initialize some moments after the JVM has created it.
In general it's a very bad idea to call methods that can be overridden from a constructor for this very reason. A method in B or C may assume that the constructor and object initializers have already been run, and read an unexpected value from a field. For example the following ends up printing "B Created!! null" because the field still has not be assigned its value.
public class B extends A
{
final String msg = "Yes!";
protected void callCreation()
{
System.out.println("B Created!! "+msg);
}
}
thinking in this way makes it more obvious:
when an object of type B is being created super() keyword calls the A constructor,
then in A constructor "this.callCreation()" is executed, which refers to the current
object which is B, so callCreation corresponding to the current object(B) is called.
the same process is done for C.
public class A {
public A() {
this.callCreation();
}
protected void callCreation() {
System.out.println("A Created!!");
}
}
class B extends A {
public B() {
super();
}
protected void callCreation() {
System.out.println("B Created!!");
}
}
class C extends B {
public C() {
super();
}
protected void callCreation() {
System.out.println("C Created!!");
}
public static void main(String[] args) {
A a = new A();
A b = new B();
A c = new C();
}
}
Related
I'm running over this problem while working with the following inheritance and JDK 14
interface A {
default void a() {
System.out.println("default a");
}
default void b() {
System.out.println("default b");
}
}
class AImp implements A {
#Override
public void a() {
System.out.println("a from AImp");
}
#Override
public void b() {
System.out.println("b from AImp");
this.a();
}
}
class B extends AImp {
#Override
public void a() {
System.out.println("a from B");
}
#Override
public void b() {
System.out.println("b from B");
super.b();
}
}
when I run
B b = new B();
b.b();
console gives
b from B
b from AImp
a from B
How come the keyword this in the AImp is referencing the instance of class B?
Am I being confused with something here?
Thank you for spending time.
You have created an instance of B, it is it's dynamic type and due to dynamic binding always the method declared in B is called, because it overrides from A.
It does not matter from where you call the method, but it matters, what is the dynamic type.
With super.method() it is different, it explicitly goes up in the inheritance.
Note: constructors are not overriden, ever. So calling this(params) will not delegate to subclass.
I wrote down this mini-program:
A class:
public class A
{
public A()
{
System.out.println(getS());
}
public String getS() { return s;}
}
B class:
public class B extends A
{
private String s = "hello2";
public String getS() { return s;}
}
main:
public static void main(String[] args)
{
B b = new B();
}
and it printed:
null
Why is that?
I know that the String that printed is B's string, but why it didn't initialized before?
According to this answer - the variable initialized before the constructor..
EDIT -
I edited the code so the unrelated code won't confuse
Here is what's going on: when you construct B, the first thing its constructor needs to do is constructing A. This is done before B's own field s is initialized.
A constructs its own s, and then calls getS. However, it does not get its own getS, because B provides an override for it. Recall that B.s has not been initialized yet. That is why you see null printed.
Follow-up reading: What's wrong with overridable method calls in constructors?
What is happening:
You create a B instance, this will call the super() so the constructor of A.
Here it will do the print using the getter getS(). This will use the getter of B since this is the type of this but in this getter, the String is not yet instanciate since it is still doing the super class construction, so it return null.
Note that the String s in A is hidden by the one in B
The order during an instance is :
the static (from the super then the class)
the super class declaration (statement then constructor)
the block statement
the constructor
As Seen with :
public class A{
static{System.out.println("sA");}
{System.out.println("A1");}
public Main() {
System.out.println("new A");
}
{System.out.println("A2");}
public static void main(String[] args) {
new A();
}
}
class B extends Main {
static{System.out.println("sB");}
{ System.out.println("B1"); }
public B() {
System.out.println("new B");
}
{ System.out.println("B2"); }
}
Output :
sA
sB
A1
A2
new A
B1
B2
new B
it prints null because you have polymorphism in java. You Overrided method getS(). So when you call it from A, you try to call getS() from class B. But you didn't create instance of class B yet, because you need to finish class A first. So String in class B haven't been initialized yet, because of it you get null.
If class A creates an instance of class B, then in class B can I run a method from class A? sorry if the question is poorly worded, I don't know how else I could say it.
In the below code, Class A creates an instance of Class B and you can call Class A's method from Class B's method.
class A {
public void getA() {
System.out.println("In A");
}
public static void main(String[] args) {
B b = new B();
b.getB();
}
}
class B {
public void getB() {
System.out.println("In B");
A a = new A();
a.getA();
}
}
Output:
In B
In A
In class B you can call methods of class A only if A's methods are visible to B. It doesn't matter who created an instance of B.
This tutorial might help: http://www.tutorialspoint.com/java/java_access_modifiers.htm
So you can call it if the method is one of the following:
public
protected (and B is a subclass of A or B is in the same package as A)
no modifier (and B is in the same package as A)
public class ExampleClass {
public static void main(String[] args) {
// Upcasting from subclass to super class.
A aRef=new C();
//aRef.setMessage(); compilation Error
aRef.display();
B bRef = (B)aRef;
//bRef.setMessage(); compilation Error
bRef.display();
C cRef = (C)aRef;
cRef.setMessage("ayaz");
cRef.display();
}
}
interface A
{
void display();
}
class B implements A
{
public void display() {
System.out.println("Am in class B");
}
}
class C extends B
{
String msg = "Am in class C";
#Override
public void display() {
System.out.println(msg);
}
public void setMessage(String s){
msg = s;
}
}
In the above code after downcasting aRef object to an object of type B unable to call setMessage(),though on calling display with bRef Obj, display method of C class is called. When we downcast to Obj of C then we are able to call why? is aRef only pointing to an object of type C, I think reference is not copied.
Two things:
at compile time the compiler looks at the type of the reference to determine if the method exists (is at least declared). So aRef.display() is correct as bRef.display() and cRef.display(). But aRef.setMessage() and bRef.setMessage() are incorrect (setMessageis not a method declared or defined in A or B. Of course cRef.setMessage() is correct.
at runtime the machine finds the correct method to execute. As aRef, bRefand cRef refer to the same C object that redefines the method display, this one is called.
Although the Object which resides in the heap is of Class C,the reference you have used is of Type B.You can only call the methods which exist in the reference and not the methods present in the actual Object.
1)Only Class C has setMessage method.So only a reference of Object C can invoke this method.Alternatively you can define these methods in interface and Class B and mark Class B as abstract with a concrete implementation in Class C
public class ExampleClass {
public static void main(String[] args) {
// Upcasting from subclass to super class.
A aRef=new C();
aRef.setMessage(); //No compilation Error now
aRef.display();
B bRef = (B)aRef;
bRef.setMessage(); //No compilation Error now
bRef.display();
C cRef = (C)aRef;
cRef.setMessage("ayaz");
cRef.display();
}
}
interface A
{
void display();
void setMessage(String s);
}
abstract class B implements A
{
public void display() {
System.out.println("Am in class B");
}
}
class C extends B
{
String msg = "Am in class C";
#Override
public void display() {
System.out.println(msg);
}
public void setMessage(String s){
msg = s;
}
}
The simple rule is:
The type of the variable tells what can be done
The type of the object tells how to do it
class a extends b {
void h() {
System.out.println("class a");
}
public static void main(String[]args) {
b x = new a();
c y = new b();
c z = new a();
x.h(); //output class a
y.h(); //output class b
z.h(); //output class a
}
}
class b extends c {
void h() {
System.out.println("class b");
}
}
class c {
void h() {
System.out.println("class c");
}
}
Whats is the precedence in which it checks which method to call. I am confused as to how the JVM decides which method to call when I use dynamic dispatch. The output in this case is
class a
class b
class a
and when I remove the overridden method from class a the output is
class b
class b
class b
The class used depends on the instantiated type. So if you have an instance of class a (via new a()), then whether the declared variable is of type a, b, or c does not matter for the purpose of which definition of h() is invoked (it will always invoke a.h()).
However, where the declared type does matter is in when choosing an overloaded method. If you have a subclass that overloads an overridden method, then when invoking from a variable declared as the parent class, you will never use the overloaded method. If using a variable declared as being of the subclass, then it will use the overloaded method as appropriate.