• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance/Subclass Query  RSS feed

 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Brand new to Java - 25 years in IT but just never used it. However, the job's market seems to be full of Java positions so I thought I'd try to learn it. Seems fairly straight forward so far but I'm now just moving into objects, inheritance etc and want to ask a quick question on the mechanics.

I have the following Modem object:


I then have subclass CableModem:


I can then test this with the following:


This displays:

Trying the cable modem...
Speed = 900000
Connecting to the internet using a cable connection
Disconnecting from a cable connection internet session


Which is correct. My question is around how the inheritance within CableModem works.

I set the value of "method" (inherited String from Modem) within CableModem to a value specific to that subclass i.e. "cable connection". My question is why does the instantiation of "method" occur within an inner "public CableMode ()" construct and not just inside the class? I actually copied this bit from somewhere as I couldn't get it to work previously. However, I'd rather know how this is working before I move on.

Cheers,

PaulC.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:My question is around how the inheritance within CableModem works.

I set the value of "method" (inherited String from Modem) within CableModem to a value specific to that subclass i.e. "cable connection". My question is why does the instantiation of "method" occur within an inner "public CableMode ()" construct and not just inside the class? I actually copied this bit from somewhere as I couldn't get it to work previously. However, I'd rather know how this is working before I move on.


It would help you elaborate a bit on your confusion. The CableModem() constructor isn't doing anything fancy. It is simply assigning a string reference to a string value.

The code is simply generated by the compiler to set the "method" variable from the superclass component of the instance. It does this because it knows that there isn't a version of the variable, that is in scope, in the subclass, and that the version in the superclass is accessible.... but ... once it is determined, the code isn't doing anything fancy. It is simply setting a variable. And this same code would work the same in a constructor, method, or initializer.

Henry
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:It would help you elaborate a bit on your confusion.


Think it all boils down to why this:


and not this:


Yes, the second gives an error. However, I'm just sure of the purpose of the inner "public CableModem ()" construct i.e. what is it doing that not having it doesn't do.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue is not related to inheritance. It is simply a syntax error. Java statements, that are not declarations, must be in a  constructor, method, or initializer.

Henry
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not that I'd recommend doing it this way but you could use an instance initializer block to make that second version work:


The block will execute whenever you instantiate CableModem so that each instance of CableModem will have their method field initially set to "cable connection". A more common way to set that up, however, would be this:

Here, the Modem class provides two ways of instantiation: via a no-argument constructor, where method will be "generic connection" by default, or via a constructor that takes the method as its argument. The CableModem class provides only one way to make instances: via the no-arguments constructor that calls the Modem(String) constructor, specifying a value of "cable connection".
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, the field name "method" can create confusion for the reader.  A better name might be "connectionType".
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:The issue is not related to inheritance. It is simply a syntax error. Java statements, that are not declarations, must be in a  constructor, method, or initializer. Henry


Thanks for the feedback. Yep, I've done a bit more reading and now realise that what I have here is an object and within its constructor:


i.e. when a NEW instance of this object is created it runs the constructor, which in this case simply sets a variable.

Cheers,

PaulC.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Not that I'd recommend doing it this way . . . .
Once you start recommending things which you wouldn't recommend, maybe you shou‍ld regard what you are recommending as a code smell. So, you have a plain simple Modem class, with a no‑args constructor, and a subclass which requires the field read “cable connection”. Now imagine you have another subclass which requires the field read “wireless connection”. Now, how are you going to arrange the constructors such that the fields all have the correct values, there is no chance of setting the wrong value, all constructors have public access and all fields have private access? And the plain simple Modem object has a field “generic connection”?
I cannot think of a straightforward solution. Moving some classes into and out of packages, and playing around with protected access and package‑private access would probably sort out the problem, but this is the beginning forum.
I can only draw one conclusion from all the confusion above: there is an error in the structure of the inheritance hierarchy. Maybe one of those classes shou‍ld be abstract.

Also, I am a bit worried by the package name. Does that mean OP has got this book, which didn't receive a good reception when it was reviewed here?
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Junilu Lacar wrote:Not that I'd recommend doing it this way . . . .
Once you start recommending things which you wouldn't recommend, maybe you shou‍ld regard what you are recommending as a code smell.

I don't see the instance initializer block as too smelly; it has a curious scent of being esoteric and of being less well-known but it's legal syntax nonetheless. Just thought I'd put it out there for consideration, not so much a recommendation.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:So, you have a plain simple Modem class, with a no‑args constructor, and a subclass which requires the field read “cable connection”. Now imagine you have another subclass which requires the field read “wireless connection”. Now, how are you going to arrange the constructors such that the fields all have the correct values, there is no chance of setting the wrong value, all constructors have public access and all fields have private access? And the plain simple Modem object has a field “generic connection”?
I cannot think of a straightforward solution. Moving some classes into and out of packages, and playing around with protected access and package‑private access would probably sort out the problem, but this is the beginning forum.
I can only draw one conclusion from all the confusion above: there is an error in the structure of the inheritance hierarchy. Maybe one of those classes shou‍ld be abstract.

Not really sure what the concern is. The code is hypothetical and doesn't really have much of a context of use on which to base a determination of appropriateness/inappropriateness. But let's say for the sake of discussion (we never argue, right? ) we do want to make some extensions. What problems do you anticipate having, given the foundation I suggested?

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You had that String field marked as final and initialised on declaration; the one‑arg constructor will surely not compile.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you take off the final modifier, what will happen?
new Modem(): generic connection
new Modem("Wireless connection"): ???
new WirelessModem(): calls super("Wireless connection").

How can we always ensure that a plain simple Modem not being sub‑typed always has "generic connection" and the subtypes always have the appropriate names for their connections while retaining private access to all fields? It might be possible in C++ where protected means in subclasses only, but the Java® combination private protected was withdrawn before Java1.1 was issued. I think it just goes to show that lots of books describe inheritance as really simple when it isn't simple at all.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You had that String field marked as final and initialised on declaration; the one‑arg constructor will surely not compile.

Serves me right for not posting compilable code. Good catch.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I think it just goes to show that lots of books describe inheritance as really simple when it isn't simple at all.
Good points all. It behooves every Java programmer to read Joshua Bloch's Effective Java Programming and what he has to say about programming for inheritance.
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Does that mean OP has got this book, which didn't receive a good reception when it was reviewed here?


Recommend me a good book....
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Already mentioned Effective Java Programming by Joshua Bloch.

I like to keep the following at arms-length or on my top shelf for easy reference:

1. Refactoring: Improving the Design of Existing Code by Martin Fowler
2. Refactoring to Patterns by Joshua Kerievsky
3. Books about JUnit, Unit Testing, and TDD (Test-Driven Development)
4. The Pragmatic Programmer: From Journeyman to Master by David Thomas and Andrew Hunt
5. Pragmatic Thinking & Learning by Andrew Hunt

E-books I keep handy bookmarks for:

1. Understanding the Four Rules of Simple Design by Corey Haines
2. Clean Code by Robert Martin
3. Agile Software Development: Principles, Patterns, and Practices by Robert Martin
4. Software Architecture for Developers by Simon Brown
5. Growing Object-Oriented Software, Guided by Tests by Nat Pryce and and Steve Freeman
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Already mentioned Effective Java Programming by Joshua Bloch.


Ok, thanks. I'll take a look.

As an aside, how important is it get a book which is based in Java 8. I have seen some recs for books written in Java 5 days. How much does the version matter to grasping the basics?

BTW I am an experienced programmer, just not in Java, thus not afraid of books not written for complete beginner/non-programmers.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't that list intimidate you just because you're new to Java.  If you have experience in other languages, it's not that hard to pick up on the mechanics of the Java language. It's the different nuances of the idioms and the way you express the behavior in code that's difficult and those books have a lot of information on how you can get to mastery quicker by avoiding pitfalls and traps that many novice Java programmers unknowingly fall into and stay imprisoned in.

I'm sure others will recommend some fine books that teach all the basics and mechanics but I would caution to take the examples in those kind of books with many grains of salt. Examples that demonstrate language constructs and mechanics seldom show good overall technique.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:As an aside, how important is it get a book which is based in Java 8. I have seen some recs for books written in Java 5 days. How much does the version matter to grasping the basics?

Not much really, unless you're wanting to bone up quickly on the newer features available in Java 8.  In that case, I like Java 8 in Action by Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft, published by Manning.  It provides good coverage of Lambdas, Streaming API, etc., and even the new Date/Time API.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:recs for books written in Java 5 days.

Yeah, I don't even look at those kinds of books, even for new languages that I try to learn.  Since you're an experienced programmer, you know you can't learn much of a language in 5 days. You learn enough to be dangerous maybe but it takes months of practice and reading to be decently conversant, years to be fluent, and forever to be great.
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Paul Clements wrote:recs for books written in Java 5 days.

Yeah, I don't even look at those kinds of books, even for new languages that I try to learn.  Since you're an experienced programmer, you know you can't learn much of a language in 5 days. You learn enough to be dangerous maybe but it takes months of practice and reading to be decently conversant, years to be fluent, and forever to be great.


I agree with what you say, but it's not what I meant...lol. I was simply saying that I've seen for books written in the Java 5 days/era...that's why I wondered whether version was truly that important to the basics i.e. objects, classes, inheritance etc
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:I agree with what you say, but it's not what I meant...lol. I was simply saying that I've seen for books written in the Java 5 days/era...that's why I wondered whether version was truly that important to the basics i.e. objects, classes, inheritance etc

I get it. My mistake. Some selective dyslexia there, I guess. Java 5 specific books are probably still good with respect to the most, if not all of, the basics of the language. The enhanced for-loop was introduced in Java 5 and there are still many programmers who don't take advantage of the more succinct syntax to write cleaner code.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and you can probably buy a second‑hand Java5 book for a good price. What Junilu suggests is a good idea: get a cheap Java5 book and use Urma Fusco and Mycroft to get up to date with Java8.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!