DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • Memory Leak Due to Uncleared ThreadLocal Variables
  • Beyond Java Streams: Exploring Alternative Functional Programming Approaches in Java
  • Java Enterprise Matters: Why It All Comes Back to Jakarta EE
  • How to Identify the Underlying Causes of Connection Timeout Errors for MongoDB With Java

Trending

  • Supercharge Your Java Apps With AI: A Practical Tutorial
  • When Caching Goes Wrong: How One Misconfigured Cache Took Down an Entire System
  • How I Built an AI Portal for Document Q and A, Summarization, Transcription, Translation, and Extraction
  • Software Specs 2.0: Evolving Requirements for the AI Era (2025 Edition)
  1. DZone
  2. Coding
  3. Java
  4. Constructors of Sub and Super Classes in Java?

Constructors of Sub and Super Classes in Java?

By 
Ryan Wang user avatar
Ryan Wang
·
Apr. 26, 13 · Interview
Likes (1)
Comment
Save
Tweet
Share
59.5K Views

Join the DZone community and get the full member experience.

Join For Free

this post summarizes some commonly asked questions from stackoverflow.com.

1. why creating an object of the sub class invokes also the constructor of the super class?

class super {
    string s;
 
    public super(){
    	system.out.println("super");
    }
}
 
public class sub extends super {
 
    public sub(){
    	system.out.println("sub");
    }
 
    public static void main(string[] args){
    	sub s = new sub();
    }
}

it prints:

super
sub 

when inheriting from another class, super() has to be called first in the constructor. if not, the compiler will insert that call. this is why super constructor is also invoked in the code above.

this doesn’t create two objects, only one sub object. the reason to have super constructor called is that if super class could have private fields which need to be initialized by its constructor.

after compiler inserts the super constructor, the sub class constructor looks like the following:

public sub(){
    	super();
    	system.out.println("sub");
    }


2. a common error message: implicit super constructor is undefined for default constructor

this is a compilation error message seen by a lot of java developers. “implicit super constructor is undefined for default constructor. must define an explicit constructor”

implicit super constructor is undefined for default constructor












this compilation error is caused because the super constructor is undefined. in java, if a class does not define a constructor, compiler will insert a default one for the class, which is argument-less. if a constructor is defined, e.g. super(string s), compiler will not insert the default argument-less one. this is the situation for the super class above.

since compiler tries to insert super() to the 2 constructors in the sub class, but the super’s default constructor is not defined, compiler reports the error message.

to fix this problem, simply add the following super() constructor to the super class, or remove the self-defined super constructor.

public super(){
    system.out.println("super");
}

3. explicitly call super constructor in sub constructor

the following code is ok:

sub-constructor-with-parameter











the sub constructor explicitly call the super constructor with parameter. the super constructor is defined, and good to invoke.

4. the rule

in brief, the rules is: sub class constructor has to invoke super class instructor, either explicitly by programmer or implicitly by compiler. for either way, the invoked super constructor has to be defined.

5. the interesting question

why java doesn’t provide default constructor, if class has a constructor with parameter(s)?

some answers: http://stackoverflow.com/q/16046200/127859



Java (programming language)

Published at DZone with permission of Ryan Wang. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Memory Leak Due to Uncleared ThreadLocal Variables
  • Beyond Java Streams: Exploring Alternative Functional Programming Approaches in Java
  • Java Enterprise Matters: Why It All Comes Back to Jakarta EE
  • How to Identify the Underlying Causes of Connection Timeout Errors for MongoDB With Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: