• Post Reply Bookmark Topic Watch Topic
  • New Topic

Clear up a few questions?  RSS feed

 
jay russ
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to answer a few questions that my book doesn't seem to be explaining. Can someone help me with these?

12. Why can a class variable of a base type be set to reference an
object of a derived type, eg:

Shape s = new Circle("Blue", 1.0);



--------------------------------------------------------------------
13. How can the methods of a derived class access private instance
variables of its base class?

--------------------------------------------------------------------
14. The following try/catch has code to explicitly handle any
IllegalArgumentException thrown. Show the modified code that
will also catch any other type of exception that could be thrown
by code called within the try block.

try
{
... methods called here ...
...
}
catch (IllegalArgumentException e)
{
... code to handle IllegalArgumentException ...
}

--------------------------------------------------------------------
15. How is the caller's flow of control (sequence of statement
execution) different when an exception is thrown from a method
that it calls, vs. when the method returns normally?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We really ask you to tell us where these questions came from...see our article on why you should QuoteYourSources.

Next, we generally don't just hand out answers. Tell us what YOU think and why, and folks will help guide you towards the correct answer, assuming you're not there already.
 
jay russ
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well for the first question would this be the answer? A variable of a base type can reference an object of a derived class because s is of type shape and circle extends shape. So s can be assigned the object since circle is related to shape.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jay russ wrote:well for the first question would this be the answer? A variable of a base type can reference an object of a derived class because s is of type shape and circle extends shape. So s can be assigned the object since circle is related to shape.

While technically you have said nothing incorrect here, as an instructor I would be looking for something more specific. Circle is related to Shape, but how? What is the nature of that relationship?
 
jay russ
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well circle inherits the state and capabilities of Shape. Your class extends the original class by adding additional methods and instance variables to represent a more specific type of the class you're building on.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jay russ wrote:Well circle inherits the state and capabilities of Shape. Your class extends the original class by adding additional methods and instance variables to represent a more specific type of the class you're building on.


So, in other words, every Circle __ _ Shape. (Fill in the blanks.)
 
jay russ
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every circle method extends shape?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jay russ wrote:Every circle method extends shape?


No. Methods don't extend types.

What I was looking for was "IS A". Every Circle IS A shape.



Because s is declared to be of type Shape, whatever SOMETIHNG is, it must be a Shape. Inheritance (extends and implements in Java) represents an "IS-A" relationship. Since every Circle IS A Shape, we can use a Circle where a Shape is expected.

Likewise, since every Circle IS AN Object, we can use a Circle wherever an Object is expected, and if Circle or any of its ancestors implements, say, Comparable, then we could say a Circle IS A Comparable, and we could provide a Circle wherever a Comparable was expected.

If you said to me, "Hand me a fruit," your requirement would be met regardless of whether I handed you an apple, a banana, or a kumquat.
 
jay russ
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can the methods of a derived class access private instance
variables of its base class?

So would the answer to this one kinda be the same thing. The derived class extends the base class so therefore it can access the parent classes instance variables.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jay russ wrote:How can the methods of a derived class access private instance
variables of its base class?


What makes you think they can?
 
jay russ
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I think it can by using super() to call the base class constructor, would this be correct?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jay russ wrote:Well I think it can by using super() to call the base class constructor, would this be correct?


No, because it's just calling the non-private super(). It's that base class constructor that's accessing the private member variables, not the subclass. If you consider that to be "the subclass having access to super's private member variables," then that means that public int getX() { return private_member_variable_x; } is "every class has access to this class's private member varibles."

Am I making sense?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again...Please tell us where these questions came from.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Again...Please tell us where these questions came from.


Seconded.

#13 in particular is really poorly phrased.
 
jay russ
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I'm not really understanding, are you saying that it's not possible?

And these are just questions my instructor has given us to answer. Sorry if they're poorly stated but that's all I have. I'm supposed to use my book but I can't find anything for the questions I've posted.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jay russ wrote:[T]hese are just questions my instructor has given us to answer.

That's all we wanted. It wasn't clear from the original post if these came from your book or what.

And you don't need to apologize for the wording of the questions, since you didn't write them.
 
jay russ
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok well i'm still having trouble understanding what you were saying for 13, not sure if you mean it is not possible?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jay russ wrote:Ok well i'm still having trouble understanding what you were saying for 13, not sure if you mean it is not possible?


I'm saying that, as worded, taken literally, it's not technically possible. And any way you interpret it that allows the subclass to access those members can also apply to allowing any other arbitrary class to access them. Subclasses get no special access to their parents' private members.

Sub cannot refer to any of Super's private members by name. It has no special access to them as a subclass. Super can choose to provide methods that expose those members values, or allow others to set them, but that's no different than how it lets other classes see or manipulate its variables through public get/set methods.

Another way of putting it: When a subclass does super(123) that is no different than some other class doing new Super(123).

You'll have to ask your instructor what he really meant by that, and what he was looking for.
 
jay russ
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I'll check in with him on that. Now the next question:

The following try/catch has code to explicitly handle any
IllegalArgumentException thrown. Show the modified code that
will also catch any other type of exception that could be thrown
by code called within the try block.

try
{
... methods called here ...
...
}
catch (IllegalArgumentException e)
{
... code to handle IllegalArgumentException ...
}


I'm not really understanding what I do here. Do I need to add code to the try or the catch or is it both? I don't even know what to put in the catch, we've never done anything like that, the only thing I've seen is something like this:

} catch (FileNotFoundException e)
{

System.out.println("Unexpected error: " + e.toString());
continue;
}
 
jay russ
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone help me with this, I'm not getting what I need to do.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jay russ wrote:Ok I'll check in with him on that. Now the next question:

The following try/catch has code to explicitly handle any
IllegalArgumentException thrown. Show the modified code that
will also catch any other type of exception that could be thrown
by code called within the try block.

try
{
... methods called here ...
...
}
catch (IllegalArgumentException e)
{
... code to handle IllegalArgumentException ...
}


I'm not really understanding what I do here. Do I need to add code to the try or the catch or is it both?


Well, you need to add something that will will also catch any other type of exception, so why don't you take a stab and post what you think you'd need to do.

I don't even know what to put in the catch, we've never done anything like that,


I don't see any instructions that tell you to put anything in the catch. Have you tried googling for something like java exceptions tutorial or java exceptions example? There's plenty of information available out there.
 
jay russ
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well it looks to me that all that needs modification is the catch block. Since it should just catch all exceptions it shouldn't matter what methods are within the try. I saw this code posted in a tutorial for "the catch-any clause":



Is this all I would have to do? The only differene is withing the parentheses of the catch.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You find that sort of thing in some books, but it is usually poor design to catch Exception alone. It is usually better to catch a specific Exception which will require specific handling.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jay russ wrote:Well it looks to me that all that needs modification is the catch block. Since it should just catch all exceptions it shouldn't matter what methods are within the try. I saw this code posted in a tutorial for "the catch-any clause":



Is this all I would have to do? The only differene is withing the parentheses of the catch.


As Campbell points out, that's generally a bad idea. But it's close to the correct solution. However:

  • Since your instructor said "any other type of exception", with a lower-case "e", you'd have to catch Throwable, not Exception. (Though I don't know if that's what he was looking for or if he's just being sloppy. Given the poor quality of the question overall, it's impossible to say.)
  • Again the imprecise wording leaves it open to interpretation, and the lack of realism and context makes it impossible to ferret out what he really is looking for, so it might be that you were supposed to just change which exception was being caught, or it might be (since he used the word "also") that you are supposed to leave the existing catch block as-is and catch everything else separately.

  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!