my dog learned polymorphism
The moose likes Beginning Java and the fly likes runtime and compile time error case confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "runtime and compile time error case confusion" Watch "runtime and compile time error case confusion" New topic

runtime and compile time error case confusion

sai panindra

Joined: Jan 06, 2009
Posts: 5
Class A{

int aVar;

public A(int a){aVar=a;}


class B extends A{

int bVar;

public B(int b){

// super(10);


public static void main(String[] args){

B b=new B(2);

In the above case, there 's a compilation error stating that the contructor A() is not present. But when I uncomment the line super(10).. the program compiles..

Please explain how this is a compilation error but not a runtime error. I presume that when a class extends another class, a super() call is made to the no arg constructor

unless another parameterized constructor of the super class is explicitly called. But I think this is known only during execution time
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Its similar to calling a method which is not defined in a Class and you would expect compiler to throw error ?

The compiler simply sees if there is constructor to call for it. In this case no arg constructor and it finds none ,as you have already have parametrized constructor.
John de Michele

Joined: Mar 09, 2009
Posts: 600

Classes get an automatic no arg constructor only if you don't create any constructors. That's why your class will compile when you uncomment the line with super() in it. The reason why it's a compile-time error is that it has no way of compiling the subclass without calling the parent class.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46424
Another way of expressing it: This is one of those things the javac tool can recognise as a problem. So it prevents you from getting into trouble by not allowing the problem beyond the compiling stage.

You should be able to find out about constructors in any decent Java book.
I agree. Here's the link:
subject: runtime and compile time error case confusion
It's not a secret anymore!