Interface and abstract class definition / difference

Click For Summary

Discussion Overview

The discussion revolves around the definitions and differences between abstract classes and interfaces in programming, particularly in the context of languages like C++ and Java. Participants explore the characteristics, usage, and implications of both concepts, touching on theoretical and practical aspects.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants propose that an abstract class is defined as having at least one abstract method and cannot be instantiated.
  • There is confusion among participants regarding the definition of an interface, with some suggesting it serves as a point of interaction between unrelated objects, while others note that interfaces cannot be instantiated and have no implemented methods.
  • A participant explains that in C++, a class member function can be declared without being defined, which raises questions about whether this constitutes an abstract class member function.
  • Another participant clarifies that declaring a class means informing the compiler without allocating memory, while defining and instantiating a class involves memory allocation.
  • Some participants mention that an interface can be viewed as a special case of an abstract class where all methods are abstract.
  • Discussion includes the implications of multiple inheritance in C++ versus single inheritance in Java, with some arguing that interfaces in Java provide a controlled form of multiple inheritance.
  • There is a note on the potential confusion between the terms "declare" and "define" in programming contexts, highlighting the nuances in terminology.

Areas of Agreement / Disagreement

Participants express varying levels of understanding and confusion regarding the definitions of abstract classes and interfaces. There is no consensus on the precise distinctions, as multiple competing views and interpretations remain present throughout the discussion.

Contextual Notes

Some participants express uncertainty about specific terms such as "instantiated" and the implications of using interfaces and abstract classes in different programming languages. The discussion reflects a range of interpretations and lacks a unified definition of the concepts involved.

lavster
Messages
213
Reaction score
0
So my understanding is an abstract class is one with at least one abstract method ( ie cannot be executed). It can therefore not bei nstantiated (not entirely sure what instantiated means)

An interface I am a bit more confused about. first of all i thought it was where unrelated objects communicate with each other, be it soft ware or hardware. ie point of interaction.
But then i read some where else that interfaces cannot be instatiated and have no implementated methods.

But this is what i said an abstract class was...

So could someone tell me what the difference between them was and perhaps give another definition of these words

Thanks
 
Technology news on Phys.org
lavster said:
So my understanding is an abstract class is one with at least one abstract method ( ie cannot be executed). It can therefore not be instantiated
In the case of C++, you can create a class member function that does nothing but just return. In C and C++, you can define the parameters for a function with a prototype, without actually defining the function, but I'm not sure if this is considerdd an abstract class member function.

lavster said:
Not entirely sure what instantiated means.
It means to actually create an instance of data or a function, one that would normally occupy the computers memory. For example you could create a typedef for a variable, class, structure, or function, but that doesn't create an actual instance of them. In the case of a function that calls itself, each call can be considered an "instance" of the function.

lavster said:
An interface I am a bit more confused about. First of all i thought it was where unrelated objects communicate with each other, be it software or hardware. ie point of interaction. But then i read some where else that interfaces cannot be instatiated and have no implementated methods.
I don't understand this. Interface is a generic term, and can mean a lot of things. In the case of hardware, the ports for I/O devices may have their own special set of addresses called "ports", such as an Intel architecture, or they may be part of the memory address spaces (called memory mapped) such as Motorola architecture. I'm not sure how to use the term interface between objects, since generally interfacing means to execute code to read/receive or write/send data for an object, and code could then "interface" between multiple objects.
 
Last edited:
lavster said:
So my understanding is an abstract class is one with at least one abstract method ( ie cannot be executed). It can therefore not bei nstantiated (not entirely sure what instantiated means)

An interface I am a bit more confused about. first of all i thought it was where unrelated objects communicate with each other, be it soft ware or hardware. ie point of interaction.
But then i read some where else that interfaces cannot be instatiated and have no implementated methods.

But this is what i said an abstract class was...
In compiler-ese, "declare" means to tell the compiler about something without causing any memory to be used by the resultant executable. "Define" and "Instantiate" mean actually cause memory to be used for something when the program is executed. When you declare a class, you simply tell the compiler something but you don't cause any code or variables to be created. When you instantiate a class, you create a block of memory within which an 'instance' of that class will reside. You might declare one class and then create many instantiations of it in your program.

When you declare a class, you can add a declaration of a member function but never bother defining that function. In C++ this is called a 'pure virtual function'. In Java, a class that has only undefined functions declared is called an 'interface'. So that class could never be instantiated because you'd get a compiler error. Interfaces are strictly for use as parent classes. You can only create child classes (that inherit) from them and those child classes MUST define all the member functions before you can instantiate one of those classes.)

On a side note people sometimes (myself included) swap the words "declare" and "define" when the context makes it clear, anyway. But technically I shouldn't be doing that.
 
Just to confirm what fleem said, whereas an abstract class is one with at least one abstract method, an interface is an abstract class with all of its methods abstract.

So the only way you can create an object in an abstract class (instantiate, or create an instance) is to create an object in a non-abstract derived class.
 
In C++, classes can have multiple inheritance, but using that "feature" in a general way can lead to very obscure bugs in the code.

In Java, classes can only have single inheritance, but that would be too restrictive for "real world" programming. You can think of an "interface" in Java as a very restricted form of multiple inheritance, such that the Java implementation can guarantee that it is being used correctly. It is impossible for the compiler and/or the run-time system to fully check the more general C++ version of multiple inheritance.
 
fleem said:
In compiler-ese, ... "Define" and "Instantiate" mean actually cause memory to be used for something ...
This conflicts with the usage of #define in C or C++, where it's used at preprocessor (before compile) time for intelligent string substitution. In it's simplest form, you can #define a name to be a value, which the pre-processor implements by substituting the string for the value each time it encounters the defined name. A #define can substitute just any string for a name. A #define string can be a macro: a series of lines, with multiple parameters to be used for string substitution, defining the operation of an inline function without having an actual instance of a function until the defined macro is actually used in the source code.
 
Last edited:

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 1 ·
Replies
1
Views
653
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
3
Views
2K