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
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
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

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Low-Code Development: Learn the concepts of low code, features + use cases for professional devs, and the low-code implementation process.

E-Commerce Development Essentials: Considering starting or working on an e-commerce business? Learn how to create a backend that scales.

Getting Started With Jenkins: Learn fundamentals that underpin CI/CD, how to create a pipeline, and when and where to use Jenkins.

Related

  • Unlocking the Power of Postgres Listen/Notify: Building Scalable Solution With Spring Boot Integration
  • The Pitfalls of Using Boolean Parameters in Methods: A Java Tutorial
  • The Long Road to Java Virtual Threads
  • Demystifying Virtual Thread Performance: Unveiling the Truth Beyond the Buzz

Trending

  • Migrating Monolith Application to Microservices
  • Node.js Unveiled: Why You Should Use It for Enterprise Apps
  • Challenges of Legacy System Integration: An In-Depth Analysis
  • Canary Releases With Apache APISIX
  1. DZone
  2. Coding
  3. Java
  4. Constructors of Sub and Super Classes in Java?

Constructors of Sub and Super Classes in Java?

Ryan Wang user avatar by
Ryan Wang
·
Apr. 26, 13 · Interview
Like (1)
Save
Tweet
Share
58.4K 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

  • Unlocking the Power of Postgres Listen/Notify: Building Scalable Solution With Spring Boot Integration
  • The Pitfalls of Using Boolean Parameters in Methods: A Java Tutorial
  • The Long Road to Java Virtual Threads
  • Demystifying Virtual Thread Performance: Unveiling the Truth Beyond the Buzz

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • 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
  • support@dzone.com

Let's be friends: