Page 1 of 1

[HD Pg 232, Sec. 9.1.2 - inheriting-constructors-and-initializers]

Posted: Tue Feb 26, 2019 3:16 pm
by OCAJO1
Not sure if it just semantics or something more to it, but when I tested the code without line 2,

Code: Select all

class Person{
   String name;
   Person(String name){
        this.name = name;
   }
}
class Employee extends Person{ // line 1
    public static void main(String args[]){
          Employee ee = new Employee("Bob"); //line 2
    }
}
The compiler complained at line 1 saying actual and formal argument lists differ in length. Later on when I added line 2, it gave the same compiler error as line 1 at line 2 as well!

Re: [HD Pg 232, Sec. 9.1.2 - inheriting-constructors-and-initializers]

Posted: Tue Feb 26, 2019 3:44 pm
by admin
Do you have a constructor in Employee class that takes a String as an argument??

Re: [HD Pg 232, Sec. 9.1.2 - inheriting-constructors-and-initializers]

Posted: Tue Feb 26, 2019 6:56 pm
by OCAJO1
Well that was kind of my point. The compiler did not even wait for line 2 to be introduced, it complained on line 1.
So if this were the exam question, and line 2 was even not present, would it be correct the answer - it will not compile?

Re: [HD Pg 232, Sec. 9.1.2 - inheriting-constructors-and-initializers]

Posted: Tue Feb 26, 2019 10:43 pm
by admin
Did you miss the section about default constructor? It explains exactly what you are asking.
Regarding whether it will compile without line 2 or not, the best way to find out is to compile it!

Re: [HD Pg 232, Sec. 9.1.2 - inheriting-constructors-and-initializers]

Posted: Wed Feb 27, 2019 1:52 pm
by OCAJO1
Yes you're right, it does hang up on line 2, not 1. I wonder what I did last time that it hung up in line 1?!
Oh well, at least the good thing is that it was just the constructors are not inherited issue.

Re: [HD Pg 232, Sec. 9.1.2 - inheriting-constructors-and-initializers]

Posted: Fri Mar 01, 2019 10:00 am
by admin
OCAJO1 wrote:
Wed Feb 27, 2019 1:52 pm
Yes you're right, it does hang up on line 2, not 1. I wonder what I did last time that it hung up in line 1?!
That's not what I meant. Line 1 will fail compilation also. The reason for that is discussed in the book is what I was saying.

Re: [HD Pg 232, Sec. 9.1.2 - inheriting-constructors-and-initializers]

Posted: Fri Mar 01, 2019 1:49 pm
by OCAJO1
I tried it again from scratch (without addition of any code from subsequent sections) and it failed on both //line 1 and //line 2. Looking at it again, that would make sense since the class already knows that, unlike its superclass, it does not have a single parameter constructor, even before any instantiation with a single parameter is attempted.