Interface and abstract class definition / difference

In summary: Using #define in this way can be a powerful tool for programming but can make reading and maintaining the source code a nightmare.
  • #1
lavster
217
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
  • #2
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:
  • #3
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.
 
  • #4
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.
 
  • #5
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.
 
  • #6
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:

What is an interface in Java?

An interface in Java is a collection of abstract methods and constants. It defines a set of methods that a class must implement in order to use the interface. Interfaces cannot be instantiated, but they can be implemented by classes.

What is an abstract class in Java?

An abstract class in Java is a class that cannot be instantiated. It is used as a base class for other classes to inherit from. Abstract classes may contain abstract methods, which are methods that are not implemented in the abstract class, but must be implemented in the subclasses.

What is the difference between an interface and an abstract class?

The main difference between an interface and an abstract class is that an interface can only contain abstract methods and constants, while an abstract class can contain both abstract and non-abstract methods. Additionally, a class can implement multiple interfaces, but it can only inherit from one abstract class.

When should I use an interface?

Interfaces should be used when you want to define a set of behaviors that a class must implement. This allows for flexibility in the implementation of those behaviors, as a class can implement multiple interfaces. Interfaces are also useful for achieving abstraction and reducing coupling between classes.

When should I use an abstract class?

Abstract classes should be used when you want to create a base class that contains common functionality that is shared by subclasses. This allows for code reusability and helps to avoid code duplication. Additionally, abstract classes can have defined methods, which can provide a default implementation for subclasses to use or override if needed.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
9
Views
5K
Back
Top