tags:

views:

908

answers:

5

My teacher gave out a practice exam on java recently and I'm curious to see how I did on the true/false part. I'm least confident about number one, so any explanation there would help a lot.

  1. In Java, a class can extend any number of abstract classes.
    false. I don't quite understand why, is it just because in an inheriting class the the parent classes could cause a conflict?

  2. In Java, it is illegal to pass a parameter whose type is an abstract class.
    False, abstract classes don't even have constructors...

  3. In Java, an abstract class can have any number of subclasses.
    True. I can't think of anything that would restrict this.

  4. In Java, there is no restriction on the number of interfaces a class can implement.
    True

  5. It is not possible to implement a stack as a doubly-linked list, because a stack requires access to only one end of the list, and a doubly-linked list provides access to both ends of the list.
    true. but it wouldn't be very efficient.

Thanks!

A: 

1: It is false because in Java you simply can't extend multiple classes.

2: I think your wrong: Abstract classes can have a constructor!

3: True, you're right. The abstract class doesn't even "know" if it gets a subclass.

4: True, you're right, there is only a theoretical limit (nothing on computers is unlimited).

5: I think you missunderstood the question: If you got a Double-Linked-List you can in fact take it and make a Stack out of it. In the List you can get the first element so you can take this method to implement the stack

theomega
He's wrong about constructors, but he gave "false", which is the right answer since the question is in the negative. Personally, I'd shoot whoever wrote the quiz, it almost seems designed to trick incorrect answers from people by switching from positive to negative questions: "True or false: it is not the case that in Java it is illegal to specify a parameter with a type which is not a non-Abstract class"...
Steve Jessop
+7  A: 

1) It is false because Java does not support multiple inheritance. A class can only extend one class, whether it is abstract or not. It can transitively extend multiple classes (e.g., it extends B which extends C so indirectly it also extends C). A class can implement multiple interfaces. There are many reasons why Java does not support multiple inheritance, but it does support multiple interfaces, so it is better in many ways.

2) First of all, abstract classes can have constructors. The claim is false because you are allowed to pass abstract types as parameters. Because of polymorphism, you will be passing a concrete subtype that you instantiated already.

3) That is true

4) That is true to a degree (there is some limit by the JVM implementation, but you'll never hit it in practice)

5) You can easily implement a stack as a doubly linked list, it's a good exercise. It is even efficient since you're still doing everything at O(1).

Uri
This isn't really homework; he already took the quiz and is not able to change what he put down. He's more or less just trying to see how he did. I see nothing wrong with answering his question, even if he didn't put his own answers up there...
DeadHead
I tried to clarify that in the text, but you're right, I'll just drop the whole thing.
Uri
+1  A: 
  1. Java has single inheritance; a class can inherit from a single parent class, whether abstract or not. You can, of course, have a chain of multiple classes inheriting from each other, e.g. abstract A --> abstract B -> C.
  2. False, but the constructor issue has nothing to do with it. Passing an argument whose type is constrained to that of an abstract class guarantees the interface for incoming objects, and is a fundamental tenet of polymorphism.
  3. True. Abstract classes tend to define partial implementations of an interface, so it would generally be a waste of time having only one implementation.
  4. True.
  5. False, surely? You could just push/pop off one end, ignoring the reverse link.
Rob
+3  A: 

1) Correct. Java (like C#) rejected multiple inheritance of implementation, mostly for complexity reasons I believe.

2) Half-correct: the actual answer was right (false; you certainly can declare parameters, variables etc of abstract classes) but your reasoning is wrong (and I suspect you meant "true" which would have been entirely wrong). Abstract classes can have constructors which are called by derived classes. Besides that, constructors are somewhat irrelevant to whether the type of a parameter can be an abstract class.

3) Correct.

4) Correct.

5) Well, you're wrong in that it is possible (whereas you claimed that it's true that it's not possible). The fact that an implementation has access to both ends of the list doesn't mean that it has to expose both ends.

Jon Skeet
I always thought that this was done because people were abusing inheritance in C++ and this was partially a way to steer them towards interfaces and composition over implementation inheritance...
Uri
That's possibly part of it - although if that were the case, then making classes and methods final by default would have been an obvious other step.
Jon Skeet
+1  A: 

1) is false, because in Java "extends" is a specific operation that inherits both all the data structures and the implementation of all methods. In Java, a class can only extend one superclass. however, in Java you can implement any number of interfaces, which only "bring in" the name and signature of functions you must implement.

The reason for this is that extended multiple base classes — which is called "multiple inheritance" — has been considered confusing. It's a little bit of a religious war issue; C++ does multiple inheritance, Java and Smalltalk don't, and Ruby instead lets you mix in modules to much the same effect as multiple inheritance.

2) You CAN pass something as an abstract class. Consider for example an abstract class Shape, with concrete subclasses Circle, Square, and Triangle. All of them are "kinds of" shape, so you would pass a Shape to any operation that should work for hem all.

3) The only restruction is that you can make a class final, which prevents it from neing subclassed. It doesn't make sense to make an abstract class final; you would never be able to use it.

4) Yup, and that's how you get away from needing to be able to extend multiple classes; interfaces are the way you get something like multiple inheritance.

5) It is certainly possible to implement a stack as a doubly-linked list. What's more, in certain situations you'd want to. Consider, for example, something where you'd like to be able to display the whole stack without disturbing it. It could then be useful to extend that stack with a non-destructive "show all" operation. In a Stack implemented on a linked list, then, you could "walk" the whole list, and "walk" it back to the head.

Charlie Martin