Sign in to follow this  
mrchrismnh

Java Inheritance Resources?

Recommended Posts

mrchrismnh    82
What I am looking for: nitty-gritty rules of Java inheritance, specifically how the superclasses of constructors work and why it is that my composited class members don't seem to be allowed to be used in extended classes. I am looking for online resources, I can find the problem out myself.

What I am not looking for: another tutorial explaining how to make my black labrador named steve say "woof woof I am Steve, a subclass of dog".

My basic issue:

public class a
{
public a(;)
protected otherclass homevec;
protected otherclass startpos;
}

public class b extends a
{
public b(super();)
public b(float x, float y, float z, float w);
{
super();
//the problem: trying to do ANYTHING with any otherclass instance gets me a java NullPointerException at runtime, for example:
awaypos._x = x;
awaypos._y = y;
}

protected otherclass awaypos;
}

Share this post


Link to post
Share on other sites
mrchrismnh    82
Because Java is not c++ and "otherclass class;" does not call the blank constructor. Leaving this up in case it helps anyone else; it is amazing how many times the act of posting a question in a public forum led me to find a solution on my own.

Share this post


Link to post
Share on other sites
Antheus    2409
[quote name='mrchrismnh' timestamp='1312480790' post='4844596']
Because Java is not c++ and "otherclass class;" does not call the blank constructor.[/quote]

Not quite. In this regard Java is exactly like C++ and even better:[code]// C++
Foo * foo;

// Java
Foo foo;[/code]
In C++ the statement is undefined and likely to crash.

The C++ equivalent of what every declaration of object does is:[code]
Foo * foo = NULL;[/code]

[quote]Leaving this up in case it helps anyone else; it is amazing how many times the act of posting a question in a public forum led me to find a solution on my own.
[/quote]

Every book on Java says: "All objects are assigned to null. New instances must be created using new operator."



What Java really does not have are value objects. So something like:[code]// C++
Foo foo;[/code]cannot be done in Java

Share this post


Link to post
Share on other sites
sjaakiejj    130
In regards to Resources on Java, one of my old lecturers wrote a book about Java, going over every important topic in the language, making extensive use of Inheritance and Generics in the later chapters. It's the book that he also uses for his lectures, and it makes an excellent resource for the language.

[url="http://www.amazon.com/Java-Just-Time-John-Latham/dp/1848900252/ref=sr_1_1?ie=UTF8&qid=1312562473&sr=8-1"]http://www.amazon.com/Java-Just-Time-John-Latham/dp/1848900252/ref=sr_1_1?ie=UTF8&qid=1312562473&sr=8-1[/url]

It's easy to read, down-to-earth, and dives much deeper into inheritance than "I'm a dog called steve".

Share this post


Link to post
Share on other sites
alnite    3436
[quote name='mrchrismnh' timestamp='1312480790' post='4844596']
Because Java is not c++ and "otherclass class;" does not call the blank constructor. Leaving this up in case it helps anyone else; it is amazing how many times the act of posting a question in a public forum led me to find a solution on my own.
[/quote]

LOL. Numerous times I found the answer [b]while[/b] typing my post.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this