Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Interface and abstract class definition / difference

  1. Oct 3, 2011 #1
    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 im 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
     
  2. jcsd
  3. Oct 3, 2011 #2

    rcgldr

    User Avatar
    Homework Helper

    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.

    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.

    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: Oct 3, 2011
  4. Oct 3, 2011 #3

    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.
     
  5. Oct 3, 2011 #4

    DrGreg

    User Avatar
    Science Advisor
    Gold Member

    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.
     
  6. Oct 3, 2011 #5

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  7. Oct 3, 2011 #6

    rcgldr

    User Avatar
    Homework Helper

    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: Oct 3, 2011
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Interface and abstract class definition / difference
  1. Simple User Interface (Replies: 7)

Loading...