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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post 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!
Last edited by OCAJO1 on Wed Feb 27, 2019 2:02 pm, edited 1 time in total.

admin
Site Admin
Posts: 10403
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Do you have a constructor in Employee class that takes a String as an argument??

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10403
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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!

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10403
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post 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.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 8 guests