• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Referring to Chapter 6 of the OCA Java SE7 Certification Guide

 
Mohammad Ali Asgar
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Reading chapter 6 - Working with inheritance - of the OCA Java SE7 Certification Guide (Programmer I) was quite a ride I enjoyed. As probably many would agree, that chapter may be one of the most interesting ones. But it certainly is the most important of all. Unless the concepts introduced in it are fully mastered, the reader will have a shakey foundation on OO. Thus, I believe the sixth chapter is most crucial for one with aspirations toward an OO career in Java. Such a person must not overlook any doubts/misconceptions and must seek clarification when it comes to OO.

Before even purchasing the book, I had prior exposure to, as well as experience of using, inheritance and related concepts in Java. The writer explains things in her own creative ways. Fusing together my knowledge of OO and the contents in chapter-6, I have got some scrambled thoughts in mind and I would like experts like Jeanne Boyarsky to kindly make comments on the following points I am raising.

On page 312 in section 6.3.2 - Using a variable of the base class to access an object of a derived class, the writer means to say that when a reference variable of a base class is used in order to refer to an instance of its derived class, ONLY the variables and methods defined in the base class can be accessed.

My comment is that it should be rephrased to make more accurate as follows:
"When a reference variable of a base class is used in order to refer to an instance of its derived class, ONLY the variables and methods inherited by the derived class can be accessed."

On page 309 in section 6.2.3 - Implementing multiple Interfaces, the writer explains that an interface is allowed to extend multiple interfaces even if they declare methods with the same name simply because there is no method body. So, the question of which of the methods the sub-interface inherits is irrelevant.

Now, my question is, what happens when a class implements two interfaces that have defined constants with the same name?

On page 144 in section 3.5.3 - Overloaded constructors, the writer says, "You cannot call a constructor from any other method in your class."

In my opinion that seems incorrect. For example, a public static factory method of a class can invoke within itself a private constructor of the same class preceded by "new", in order to return an instance of the class.

Thank you for reading through. Please share your comments.

__ softwarelover
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammad Ali Asgar wrote:On page 312 in section 6.3.2 - Using a variable of the base class to access an object of a derived class, the writer means to say that when a reference variable of a base class is used in order to refer to an instance of its derived class, ONLY the variables and methods defined in the base class can be accessed.

My comment is that it should be rephrased to make more accurate as follows:
"When a reference variable of a base class is used in order to refer to an instance of its derived class, ONLY the variables and methods inherited by the derived class can be accessed."

Wrong! The original statement in the book is 100% correct. You can easily verify this with a small code snippet.


Mohammad Ali Asgar wrote:Now, my question is, what happens when a class implements two interfaces that have defined constants with the same name?

That's a good question But something you can test for yourself with another small code snippet. I'll gave you the 1st one for free, this one is up to you

Mohammad Ali Asgar wrote:On page 144 in section 3.5.3 - Overloaded constructors, the writer says, "You cannot call a constructor from any other method in your class."

In my opinion that seems incorrect. For example, a public static factory method of a class can invoke within itself a private constructor of the same class preceded by "new", in order to return an instance of the class.

I think the statement in the book is also correct on this one, but it's a subtle difference. You are refering to creating new instances (using the new keyword) and that's indeed possible (otherwise we can't create objects/instances from our classes). The author is referring to invoking a constructor just like you would invoke a method (so without the new keyword). As shown in this code snippet:


Hope it helps!
 
Mohammad Ali Asgar
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Roel,

Thanks for taking the time to reply, touching all my points. But I do not see how your first snippet disproves my line when I say, "... only variables and methods inherited by the derived class can be accessed"

Of course the compilation will fail when methodB is called on a B object being referred to by an A reference variable! The method methodB is NOT an inherited method. Class B inherits methodA. So, a reference variable of type A can access and run methodA on an instance of class B since class B has inherited methodA from class A. Therefore, an attempt to run methodB fails!

___ softwarelover
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammad Ali Asgar wrote:But I do not see how your first snippet disproves my line when I say, "... only variables and methods inherited by the derived class can be accessed"

My mistake! I misread your statement with my sleepy eyes and didn't notice you changed "defined" (in original statement) by "inherited" (in your statement).

But the original statement is still correct (and your's is not). As shown in this code snippet (methodA defined in base class and not inherited, but it can still be invoked):
 
Mohammad Ali Asgar
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,

Thanks for your example that shows a piece of code compiles. But, you put a right idea (because it compiled) into a wrong context (because it had nothing to do with effects of inheritance). I have noticed that you cleverly took out the main() method of a class C and placed it into a class A; the same class that defined the private method methodA(). So, when main() ran, it did so (rightly) from within class A. As a result, the code compiled, not because of any inheritance phenomenon we were discussing, but because of Java language specifications as can be seen from the reference below.

Please read this portion on the link below:

Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (ยง7.6) that encloses the declaration of the member or constructor.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.6.1

Your code basically demonstrated a special situation that happens to fit specifications outlined in JLS 6.6.1; main() belonging to the same class as the private member it accesses. Please try running main() as a member of class C, as it was originally, and see what happens. :-)

See, all I wanted to say is that the line in the book should not have meant to generalize by saying that "...can access everything defined in the base class." Or, it should have highlighted the special cases by including additional lines next to the generalized idea....

__ softwarelover
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammad Ali Asgar wrote:But, you put a right idea (because it compiled) into a wrong context (because it had nothing to do with effects of inheritance).

The only context I was aiming for was disproving your statement "When a reference variable of a base class is used in order to refer to an instance of its derived class, ONLY the variables and methods inherited by the derived class can be accessed." and I succeeded Disproving a statement is not that hard: all you need to do is finding 1 example that contradicts with the statement and you're done.

Important to remember: at compile time it's decided which methods you can invoke (based on the type of the reference variable), which method is invoked is decided at run-time (based on the type of the object the reference variable is refering to, polymorphism as you know). That's also the reason why the original statement in the book is correct (and your suggestion is wrong).
 
Mohammad Ali Asgar
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I do not think you understood the essence of my last post. Anyway, I thank you for touching all the remaining points I had originally raised. But, the last one, no, I cannot agree.

Anyway, further discussion on this forum seems unnecessary. I will try to contact the writer directly and talk to her.

__ softwarelover
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammad Ali Asgar wrote:I do not think you understood the essence of my last post.

In your original post you refer to a section and statement from Mala Gupta's point about Using a variable of the base class to access an object of a derived class and your comment was that this statement should be rephrased to make it more accurate. So I just wanted to show you that your proposal of rephrasing the original statement is just wrong. If you use a reference variable of a (base) class, you can only access variables and methods defined in this (base) class (with regard to their access modifiers of course). Always! No matter the type of the object this reference variable is referring to. That was my one and only concern.
 
Mala Gupta
Author
Ranch Hand
Posts: 363
11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel,
Thank you for anwering the queries in this thread.


Ali,

Let's revisit your understanding of section 6.3.2 and your suggestion:

This what you think the book states:
"When a reference variable of a base class is used in order to refer to an instance of its derived class, ONLY the variables and methods defined in the base class can be accessed.

Your suggestion:
"When a reference variable of a base class is used in order to refer to an instance of its derived class, ONLY the variables and methods inherited by the derived class can be accessed."

Here's the example code in question:


So what happens when a reference variable is used to refer to an object that isn't its own type:

Employee emp = new HRExecutive();

When you compile the preceding code, the Java compiler checks the definition of class HRExecutive, to confirm whether 'emp' can refer to an object of 'HRExecutive'. Because 'HRExecutive' extends 'Employee', the compiler allows this code.



Now, what happens when you try to access a variable or call a method using the reference variable 'emp':





The Java compiler refers to the definition of class Employee to determine whether it defines a variable 'name' (does this variable exist), which can be accessed in another class 'Office' (is it accessible)? Because 'Employee' defines 'name' with default access, 'emp.name' compiles (we assume Employee and Office are defined in the same package). But 'emp.specialization' fails compilation because 'Employee' doesn't define 'specialization'.

Even though Java compiler refers to the definition of class HRExecutive when you try to use 'emp' to refer to an instance of 'HRExecutive', it doesn't refer to 'HRExecutive' to compile code that accesses variables or methods from 'emp'. So inheritance doesn't come into picture here.

Hope this addresses your concerns.

With respect,
Mala
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic