This week's book giveaway is in the Web Services forum.
We're giving away four copies of Microservices in Action and have Morgan Bruce & Paulo A. Pereira on-line!
See this thread for details.
Win a copy of Microservices in Action this week in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Basic Question.  RSS feed

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The following question was in a mock paper. Could you please tell me why it is ending in compilation error? The error says:

TestAll.java: 5: cannot find symbol
symbol : constructor TestSuper()
location: class TestSuper
class TestSub extends TestSuper{ }




class TestSuper
{
TestSuper(int i) { }
}
class TestSub extends TestSuper{ }
class TestAll
{
public static void main (String [] args)
{
new TestSub();
}
}
 
Java Cowboy
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question has to do with the following rule.

Java Language Specification section 8.8.9:

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.

  • A compile-time error occurs if a default constructor is provided by the compiler but the superclass does not have an accessible constructor that takes no arguments.


    Now, let's look at your example.

    Your class TestSuper has a constructor that takes an int. The compiler will not automatically add a default constructor to class TestSuper, because there is already a constructor in the class.

    Your class TestSub does not have a constructor declaration. So the compiler will automatically add a default constructor to class TestSub, which will invoke TestSuper's no-args constructor.

    The problem is in that last part: class TestSuper doesn't have a no-args constructor, so you get an an error.
    [ August 19, 2007: Message edited by: Jesper Young ]
     
    Suresh Rajadurai
    Ranch Hand
    Posts: 58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Jasper,

    Thank you so much. I really got the exact picture of the problem. I appreciate your reply. Thank you once again.

    Suresh
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!