What is a prototype?

In a lecture on Java, a computer science professor argues that Java class interfaces are prototypes for public methods, as well as descriptions of their behavior.

enter image description here
(Source https://www.youtube.com/watch?v=-c4I3gFYe3w @ 8: 47)

And at 8:13 in the video, he says to go to the discussion section with the teaching assistants to find out what he means by the prototype.

What does “prototype” mean in Java in the above context?

+7
java
source share
7 answers

I think the use of the word prototype in this context is unsuccessful, some languages, such as JavaScript, use something called prototypal inheritance, which is completely different from what is discussed in the lecture. I think the word “contract” would be more appropriate. The Java interface is a language function that allows the class author to declare that any specific implementations of this class will provide the implementation of all methods declared in any interfaces that they implement.

It is used to allow Java classes to form multiple is-a relationships without resorting to multiple inheritance (not permitted in Java). You may have the Car class inherited from the Vehicle class, but it implements the Product interface, so the Car is both a Car and a Product.

+8
source share

What does “prototype” mean in Java in the above context?

The word "prototype" is not standard Java terminology. It is not used in JLS, and it is not mentioned in the Java Tutorial Glossary . In short, there is no Java-specific meaning.

Your lecturer uses this word in a broader sense, rather than in a Java-specific sense. In fact, its use is consistent with the “function prototype” as described on this Wikipedia page .


Unfortunately, the language "IT English" is full of examples where a word or phrase means different (and sometimes contradictory) things in different contexts. There are other meanings for a "template" that you will meet in IT. For example:

  • In C ++, “pattern” refers to the fact that Java calls a generic class or method.

  • In Javascript, an object has a template attribute that provides object methods.

  • More generally, template-based typing is an alternative (more dynamic) way to enter OO.

But the fact that these values ​​exist does not mean that your lecturer mistakenly referred to interface method signatures as "templates."

+4
source share

"prototype" is not the best / correct end to be used. interfaces more like “contracts” that implementing classes should execute.

Manuscripts / method definitions must be implemented in the implementation class (using the implements keyword in the class / class / public class xy implements ... definition).

I suggest that these naming conventions leave a lot of room for many ideological discussions.

Or the author had some kind of mental laps and somehow juxtaposed the construction of prototypical inheritance from javascript in java in his mind.

+3
source share

Interfaces are not prototypes for classes in Java.

In languages ​​such as C and C ++, which are compiled directly into machine code, the compiler must know the nature of any identifier (variable / class / function) before they are referenced anywhere in the program. This means that these languages ​​must know the nature of the identifier in order to generate the output of the machine code that is associated with it.

In simple words, the C ++ compiler needs to know the methods and member of the class before this class is used anywhere in the code. To achieve this, you must define the class in front of the line of code where it is used, or you must at least declare its nature. Declaring only the nature of a function or class creates a 'prototype'.

In Java, an “interface” is something like a class description. This determines which all methods should have a particular class type. Then you can create classes that implement this interface. The main purpose that interfaces serve in java is the possibility that a variable declared as a specific type of interface can contain objects of any class that implements the object.

+2
source share

He talks about it in C/C++ way, let me explain, in C++ you can define prototypes for methods in class header files so that other classes can recognize these methods, also in C where there is no class, you can define prototypes at the beginning of the file and then somewhere in the same file you can implement these prototypes so that the methods can be used even before they are implemented. Thus, in Java interfaces Java provide almost the same thing; you can define prototypes for methods (method headers) that will be implemented by classes that implement this interface.

+2
source share

In a lecture on Java, a computer science professor states that:

Java class interfaces :
1. prototypes for public methods,
2. plus descriptions of their behavior.

For 1. Good: yes, these are prototypes for implemented public methods of the class.
For 2. This part can be a bit complicated. :)

  • why?
  • we know: interface definition (contains prototypes), but does not define (describe) the behavior of methods.
  • The computer science professor states: "... plus descriptions of their behavior." This is only correct if we consider a class that implements this interface (implementation of an interface = prototype definitions or descriptions).

Yes, a little hard to understand :)

Bibliography:
Definition vs Description
Context-dependent
Name Visibility - C ++ Tutorials


Extrawork:

Note: not verified, just think! :)

C ++:

 // C++ namespace just with prototypes: // could be used like interface similar with Java? // hm, could we then define (describe) prototypes? // could we then inherit namespace? :) namespace anIntf{ void politeHello(char *msg); void bigThankYou(); } 
+1
source share

Prototypes provide function signatures that you will use in your code. They are somewhat optional, if you can order your code, so that you only use the functions that were previously, then you can leave without defining them.

Below is a prototype of a function that sums two integers.

 int add(int a, int b); 
+1
source share

All Articles