Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1023329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:36:03+00:00 2026-05-16T11:36:03+00:00

The following code gives a compilation error: class parent { parent(int a){} } class

  • 0

The following code gives a compilation error:

class parent {
  parent(int a){}
}

class child extends parent{}

Error:

Main.java:6: cannot find symbol
symbol  : constructor parent()
location: class parent
class child extends parent{}
^
1 error

I was trying to do different things and found that adding a return type to the parent constructor got rid of the error!!!

class parent {
  int parent(int a){}
}

class child extends parent{}

I’ve read that constructors should not have return type, which clearly is not correct all the times. So my question is when should we have return type for constructor?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:36:03+00:00Added an answer on May 16, 2026 at 11:36 am

    On constructor not having return type

    Constructor must not have a return type. By definition, if a method has a return type, it’s not a constructor.

    JLS 8.8 Constructor Declarations

    A constructor is used in the creation of an object that is an instance of a class. [The name must match the class name, but], in all other respects, the constructor declaration looks just like a method declaration that has no result type.


    On default constructors

    The following snippet does give a compilation error:

    class Parent {
       Parent(int a){}
    }
    
    class Child extends Parent{
    
       // DOES NOT COMPILE!!
       // Implicit super constructor parent() is undefined for default constructor.
       // Must define an explicit constructor
    
    }
    

    The reason is not because of a return type in a constructor, but because since you did not provide ANY constructor for Child, a default constructor is automatically created for you by the compiler. However, this default constructor tries to invoke the default constructor of the superclass Parent, which does NOT have a default constructor. THAT’S the source fo the compilation error.

    Here’s the specification for the default constructor:

    JLS 8.8.9 Default Constructor

    If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided:

    • If the class being declared is the primordial class Object, then the default constructor has an empty body.
    • Otherwise, the default constructor takes no parameters and simply invokes the superclass constructor with no arguments.

    The following is a simple fix:

    class Parent {
       Parent(int a){}
    }
    
    class Child extends Parent{
    
       // compiles fine!
       Child() {
          super(42);
       }
    
    }
    

    On methods having the same name as the constructor

    The following snippet DOES compile:

    // COMPILES FINE!!
    
    class Parent  {
    
       // method has same name as class, but not a constructor
       int Parent(int a) {
          System.out.println("Yipppee!!!");
          return 42;
       }
    
       // no explicit constructor, so default constructor is provided
    }
    
    class Child extends Parent {
    
       // no explicit constructor, so default constructor is provided
    
    }
    

    There is in fact no explicit constructor in the above snippet. What you have is a regular method that has the same name as the class. This is allowed, but discouraged:

    JLS 8.4 Method Declarations

    A class can declare a method with the same name as the class or a field, member class or member interface of the class, but this is discouraged as a matter of style.

    You will find that if you create a new Parent() or a new Child(), "Yipppee!!!" will NOT be printed to standard output. The method is not invoked upon creation since it’s not a constructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that following code gives compilation error : class A{ public : virtual
The following C++ code gives an error while compiling: #include<iostream> using namespace std; class
The following code gives an compilation error for void b() { m = &A::a;
Can anybody explain the working of following code...? interface myInterface{} public class Main {
Why does the following code not give me a duplicate symbol linker error for
Following code gives compiler error which is expected ( Demo ): 1 template<bool> struct
I have the following code: #include <iostream> using namespace std; class testing{ int test()
Consider the following code: class C { public: int operator-(int x) { return 3-x;
The following code gives digits whose name is shorter than their value. I can't
The following code gives seg fault and some time stalls/hangs and never works. for(i=0;i<360;i++)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.