• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusing word problem  RSS feed

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this time I actually did the coding by myself and it turned out great! (GUI slot machine See!!!)

But this was thrown in to the lab and these always stretch my brain and confuse me

Part A. Answer the following questions. No handwritten answers will be accepted.
Assume that classes are defined with the following class headers (hint: draw yourself a picture):
public class A
public interface B
public class C extends A implements B
public class D extends A
public class E extends C
Further assume that a method foo() is defined with the same signature in classes A, C, and E, but
not in D or B. Also assume the following declarations:
C obj1 = new E();
A obj2 = obj1;
C obj3 = new C();
A obj4 = obj3;
For each method invocation below, first clearly indicate whether or not it will compile. If it will
compile, indicate the class that defines the foo method that will be executed by that statement.
1. obj1.foo();
2. obj2.foo();
3. obj3.foo();
4. obj4.foo();


I have a picture with all sorts of arrows on it, but I'm not sure if I'm looking at this correctly.
obj1.foo would be defined by the foo in E and obj3.foo would be defined by the foo in C right?
Then the obj2 and obj4 would run the same foo as the obj1 and obj3 that they are pointing to? Is it possible for A to reach that far down?
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christina Bremmerman wrote:Is it possible for A to reach that far down?

I'm not sure if this is what you're asking, exactly, but the instances of any class can be assigned to references of any ancestor class in the inheritance tree -- up to and including Object.
 
Christina Bremmerman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I think I don't have enough of a grasp on this to know what I should even be asking.
I think this picture drawing thing isn't going to help, I'm just going to try to write it out as a program to answer it
 
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christina Bremmerman wrote:I'm just going to try to write it out as a program to answer it

That sounds like a good way to test it. Let us know what your findings are.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what happened when you drew the diagram?
 
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is needlessly complicated. They are trying to trick you

ETA:
Is it possible for A to reach that far down?



There is no down and up in inheritance, although we usually draw base classes above inherited classes in diagrams. Maybe you are looking at it the wrong way. When you say" class C extends A" it means C is A. C is not under A. It's like saying a dog is an animal. Dog is not under Animal.. It is an Animal. How the dog behaves won't change whether you call it a dog, or you call it an animal. It's going to bark either way. Similarly if you take an instance of C, and you call foo on it, it will do what C's foo does, doesn't matter whether you it's reference in a variable of type C, or type A.
 
Christina Bremmerman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure I'm doing the test code right either. I couldn't get the obj2 or 4 in A to compile they aren't able to see obj1 and 3 which I think should be right as far as the question goes because if we go to the dog and animal all animals aren't going to bark right? here is my code for A though in case someone can see that I did something wrong and it's not compiling because of me and not because that's what the answer is...



I did C the same way and it compiled



This is how I'm trying to test the foos out, but I think I completely lost it with this. I feel like I really need to get over the foos not making any sense because if this were animals maybe it would have clicked a little bit better




And these are my B and D even though I don't think they're relevant for this problem...

 
Christina Bremmerman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And what happened when you drew the diagram?


It made no sense to me.
From drawing a few boxes and a bunch of arrows I really don't know what to look for to find what will work and what isn't going to compile and what method will come from where. The example wasn't this complicated at all so it's hard for me to match up the lecture and my notes to this problem for anything more than where the arrows are going. If that makes sense?
 
Christina Bremmerman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know why I put the doFoo() in there but I took that out of A and C and creaded the objs in my test drivers and now it's all compiling, I think I'm seeing the light...LoL hopefully that and not just adding to my confusion


So now, everything compiles and obj1 and obj2 are using E foo() and obj3 and obj4 both use C foo()

Which actually is what I thought I saw in my diagram, but that seamed too easy...and it still kind of does...
Can anyone see anything wrong with my answer that I might be missing?
This prof likes to be tricky and I always miss stuff like this because I get tricked easily...
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christina Bremmerman wrote:I don't know why I put the doFoo() in there but I took that out of A and C and creaded the objs in my test drivers and now it's all compiling, I think I'm seeing the light...LoL hopefully that and not just adding to my confusion


So now, everything compiles and obj1 and obj2 are using E foo() and obj3 and obj4 both use C foo()

Which actually is what I thought I saw in my diagram, but that seamed too easy...and it still kind of does...
Can anyone see anything wrong with my answer that I might be missing?
This prof likes to be tricky and I always miss stuff like this because I get tricked easily...


You got it. Glad to see you worked your code out, and went back to verify your diagram.

The professor did try to trick you by introducing D and B. D and B don;t really matter to the questions asked. They are just there to make your diagram complex.

Christina Bremmerman wrote:I feel like I really need to get over the foos not making any sense because if this were animals maybe it would have clicked a little bit better


Try not to make a foo of yourself :lol:

Yes, IMO, until you get a hang of something new, taking concrete examples works better than using abstract ones. Being able to understand concepts in abstract terms comes eventually.
 
Christina Bremmerman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help! We went over it in class yesterday and it turned out he put B and D in there because he meant to add B obj.foo and D obj.foo but he forgot to so the problem was easier than he intended.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!