tags:

views:

1484

answers:

8

I'm working through some homework and a question on a previous exam paper asks to name all of the abstract classes in a given UML diagram. Fairly straightforward, I suppose. There is one abstract class and three interfaces. Do these interfaces qualify as abstract classes, in general?

A: 

I'd say anything that does not implement real code is abstract, so: Yes.

Tomalak
Except an abstract class in java can implement real code. You just can't instanciate an object of an abstract class.
Ron Tuffin
And also in C#, an abstract class can implement real code just like java
yapiskan
Tomalak didn't say that abstract things don't implement code, he said things that don't implement code are abstract. What he said is not contradicted by the existence of abstract classes which implement code. Yes, I did take classes in predicate calculus, since you ask ;-)
Steve Jessop
Thank you, onebyone. ;-) Predicate calculus is not even necessary to see that, but it sure helps. :-D
Tomalak
+1  A: 

Yes, an Interface is implicitly Abstract. Look behind the scenes as to the way it is encoded to a .class file.

Semantics are a funny thing though; under exam conditions "abstract class" would have to literally be compiled from a .java source file using abstract class in the Class' declaration.

_ande_turner_
+4  A: 

All interface are indeed abstract

Actually, you can declare an method as abstract within an interface... except any 'checkstyle' tool will tell you the abstract keyword is redundant. And all methods are public.

If a class implements an interface and does not implement all its methods, it must be marked as abstract. If a class is abstract, one of its subclasses is expected to implement its unimplemented methods.

To echo other answers, an interface is not a class.

An interface is a reference type, similar to a class, that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.

Interfaces are not part of the class hierarchy, although they work in combination with classes.

When you define a new interface, you are defining a new reference data type. You can use interface names anywhere you can use any other data type name. If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface


To better explain why an interface is not a class, consider the following:

1/ an interface is a type used by values

2/ a class is for Objects

3/:

Object a = new Date();
String s = a.toString();
  • The type of the variable 'a' is Object (which is actually a type notation in Java source code meaning a reference to an Object),
  • but the class of the object it points to is Date.

The type (Object) only affects what code is valid according to the compiler's type-checking, but not what the code actually does.

The class of the object affects what the code does, so that the a.toString() call in the second line returns a String that looks like a Date, not one that looks like "java.lang.Object@XXXXXXXX".

Since an Interface is a type, it is used for values only, and will not represent what objects will actually do in term of runtime.

VonC
+18  A: 

Thing is, while technically interfaces may be represented as classes in languages like Java, I wouldn't consider them classes.

Abstract? Hell yes. Class? No.

Interfaces cannot have constructors, neither properties, fields, function bodies, etc. Interfaces cannot be inherited, they are implemented (again, technically it might be true that implementing an interface is actually inheriting it in specific languages, but that's not my point.) Interfaces are more like 'contracts' as they do not define any behaviour whatsoever like classes.

Now if this is a homework then you shouldn't really argue about these sort of stuff with the teacher. Just check your lecture notes and see if the word "class" is mentioned anywhere in the your teacher's definition of interface.

DrJokepu
+1  A: 

You've only asked about the abstract side, but don't forget the class side - I wouldn't call an interface a class, so even though interfaces are abstract (as per the specification), I still don't think they count as abstract classes. It may be worth explicitly explaining that though :)

Jon Skeet
+3  A: 

In Java though, theres a twist to the tale - all Interfaces in Java extend java.lang.Object! Try adding a method:

public void notify();

in an interface and see what happens..

An Interface extending a Class - Does that make the Interface a Class? Or the Class an Interface?? Uhh-huh.. Guess it was a hack that had to be done to prevent interfaces overriding the definitions in java.lang.Object that implementations of the interface had to extend anyway.

Gowri
A: 

An interface contains prototype of methods (i.e Declaration ) not defination but Abstract class can contain defination of method & atleast one Abstract method (method with only prototype)

kedar kamthe
A: 

Interfaces are used to break the Object Inheritance. They could hold two or more objects of several classes and classes hierarchies. Look at an interface as an outlet plug. All classes implementing an Interface need to have one, the same way a computer, a coffee machine, a ventilator and a refrigerator need to have the same device to get power.