How to call a method from Constructor

0 votes
public abstract class A
{
  public A()
  {
    this.load();
  }

  protected void load()
  {

  }
}

public class B extends A
{
  private String testString = null; 

  public B()
  {
    super();
  }

  @Override
  protected void load()
  {
    testString = "test";
  }
}

The application is doing this when creating an instance of the class B using a load class by name method:

  • Calls overridden load() in class B
  • Initializes variables (calls "private string testString = null" according to debugger), nulling them out.

Is this expected Java behavior? What could cause this? It's a Java 1.6 application running on the 1.7 JDK.

Nov 13, 2018 in Java by Neha
• 6,300 points
6,472 views

1 answer to this question.

0 votes

Have a glance at below steps:

  • You create an instance of B.
  • B() calls superclass constructor - A(), to initialize the superclass members.
  • A() now invokes a non-final method which is overridden in B class, as a part of initialization.
  • Since the instance in the context is of B class, the method load() invoked is of B class.
  • load() initializes the B class instance field - testString.
  • The superclass constructor finishes a job, and returns (Assuming chaining of constructor till Object class have been finished)
  • The B() constructor starts executing further, initializing its own member.
  • Now, as a part of an initialization process, B overwrites the previously written value in testString and re-initializes it to null.

answered Nov 13, 2018 by Frankie
• 9,830 points

Related Questions In Java

0 votes
1 answer

How to call a method after a delay in Android using Java?

final Handler handler = new Handler(); handler.postDelayed(new Runnable() ...READ MORE

answered Jun 11, 2018 in Java by Akrati
• 3,190 points
5,078 views
0 votes
2 answers

How to return multiple objects from a Java method?

In the event the method you're calling ...READ MORE

answered Aug 30, 2019 in Java by Karan
• 19,610 points
8,274 views
0 votes
1 answer

How to call a method in java

In Java, you call a method by ...READ MORE

answered Oct 13, 2023 in Java by Aditya
182 views
0 votes
1 answer

Is it possible to run a java program from command line on windows?How?

  Let's say your file is in C:\myprogram\ Run ...READ MORE

answered Apr 18, 2018 in Java by sophia
• 1,400 points
2,371 views
0 votes
1 answer

Can Abstract Class have a Constructor?

Yes, an abstract class can have a ...READ MORE

answered May 11, 2018 in Java by sharth
• 3,370 points
906 views
0 votes
2 answers

How to call one constructor from another in Java?

public class Cons { public static Cons ...READ MORE

answered Jul 24, 2018 in Java by Akrati
• 3,190 points
1,316 views
0 votes
1 answer

How to set a timer in Java?

To work on timer in java, you ...READ MORE

answered May 16, 2018 in Java by Daisy
• 8,120 points
992 views
0 votes
2 answers

When and how to use Super() keyword in Java?

super() is used to call immediate parent. super() can be ...READ MORE

answered Jul 9, 2018 in Java by Sushmita
• 6,910 points
1,572 views
0 votes
1 answer

How to create a temporary directory/folder in java?

If you are using JDK 7 use ...READ MORE

answered Aug 28, 2018 in Java by Frankie
• 9,830 points
6,094 views
+1 vote
1 answer

How to calculate days between two dates?

You are making some conversions with your ...READ MORE

answered Aug 28, 2018 in Java by Frankie
• 9,830 points
2,601 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP