2

I am trying to pick up the basics of Java and I am more familiar with JavaScript.

Is the following statement accurate (I just need high level understanding):

Javascript constructor function or factory function is the equivalent (i am using this word loosely here) of a class or interface in Java.

EDIT:

This is what I am reading in a Java book:

A Java program is mostly a collection objects talking to other objects by invoking each other's methods. Every object is of a certain type, and that type is defined by a class or an interface. Most Java programs use a collection of many different types.

Coming from Javascript, above sounds very much like JS constructor function is similar to a class in Java where the objects properties and methods would be defined.

I know Java and JavaScript are two separate languages.

Thanks

Andy
  • 541
  • 2
  • 4
  • 14
  • Java classes have constructors, if that is what you are asking. Java interfaces do not. – crush Jan 09 '14 at 17:09
  • 1
    @crush OP is speaking of constructors in *JS*, not saying Java classes and interfaces have constructors. –  Jan 09 '14 at 17:10
  • 1
    It sounds to me like he's asking if a JS constructor is equivalent to a class or an interface in Java. – crush Jan 09 '14 at 17:10
  • @crush Yes, that's my understanding as well. But in that case your comment makes no sense. –  Jan 09 '14 at 17:11
  • @delnan I don't see how it doesn't make sense. A JS object with a constructor is closer to resemblance of a Java class which also has a constructor than a Java interface which does not have a constructor. – crush Jan 09 '14 at 17:13
  • I don't think anything in JavaScript is equivalent to anything in Java. Except that the syntax is sort of kind of similar in some places. But the paradigm is so completely different that I don't think you can refer to your JavaScript experience to get an understanding of Java, C#, C++, etc. – ajb Jan 09 '14 at 17:14
  • 1
    @crush The thing is, "constructor" means rather different things in JS and Java, even if the two may be comparable (of which I am not sure). You appear to be confusing two concepts which share the same term. –  Jan 09 '14 at 17:14
  • A constructor serves to initialize an object upon instantiation. Sure, they are implemented differently in JS and Java. Quite a bit differently. They serve the same purpose. I'm not sure what you're getting at. – crush Jan 09 '14 at 17:17

5 Answers5

6

I'd say you're close. Constructor functions (and prototypes) in JavaScript are the closest thing to Java classes that we have in JS; but they're certainly not "equivalent".

You can dynamically add or remove properties and methods to the prototype of a JavaScript constructor; you can't add or remove things from a Java class at runtime.

Example:

function Foo() {}
Foo.prototype.hello = function() { alert('hello'); };

var f = new Foo();
f.hello(); // alerts 'hello'

delete Foo.prototype.hello;

f.hello(); // throws an error

You can achieve "inheritance" at runtime in JavaScript simply by assigning the prototypes of constructor functions to arbitrary objects. In Java you declare inheritance at compile-time and it cannot be changed at runtime.

Example:

function EnglishSpeaker() {}
EnglishSpeaker.prototype.greet = function() { return 'hello'; };

function SpanishSpeaker() {}
SpanishSpeaker.prototype.greet = function() { return 'hola'; };

function Me() {}
Me.prototype = EnglishSpeaker.prototype;

var me = new Me();
me instanceof EnglishSpeaker; // true
me.greet(); // 'hello'

Me.prototype = SpanishSpeaker.prototype;
me = new Me();
me instanceof EnglishSpeaker; // false
me instanceof SpanishSpeaker; // true
me.greet(); // 'hola'

In JavaScript a prototype is simply an object. So a "class" (constructor function) can "inherit" from any plain object; thus there is a much looser distinction between "types" and "values".

Example:

function Thing() {}

var randomObject = { foo: 1, bar: 2 };
Thing.prototype = randomObject;

var thing = new Thing();
thing.foo; // 1

In Java you can define an interface which some class must implement. JavaScript doesn't really provide any such mechanism.

These are just some of the differences off the top of my head. Point is: they're similar, and you're right to draw a connection. But they are definitely not the same.

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
4

JavaScript:

function Cat(name) {
    this.name = name;
    this.talk = function() {
        alert( this.name + " says meeow!" );
    };
} 

var cat1 = new Cat("Felix");

in Java:

public class Cat {
    private String name;

    public Cat(String name) {
        this.name = name;
        this.talk();
    }

    public void talk() {
        System.out.println( this.name + " says meeow!" );
    }
} 

Cat cat1 = new Cat("Felix");
Ry-
  • 218,210
  • 55
  • 464
  • 476
Ben
  • 391
  • 5
  • 13
  • 1
    The Java here isn't valid. [This paste](http://paste.ubuntu.com/6721854/) shows it corrected and made to follow Java naming conventions. – nanofarad Jan 09 '14 at 17:17
  • thanks, updated my post. totally forgot about public and void :D – Ben Jan 09 '14 at 17:20
  • You can emulate JS's `arguments` paramater in the java constructor with [Varargs](http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html) `public Cat(String name,Object...arguments)` –  Jan 09 '14 at 17:26
0

From source :Does JavaScript have the interface type (such as Java's 'interface')?

JavaScript inheritance is based on objects, not classes. That's not a big deal until you realize: JavaScript is an extremely dynamically typed language -- you can create an object with the proper methods, which would make it conform to the interface, and then undefine all the stuff that made it conform. It'd be so easy to subvert the type system -- even accidentally! that it wouldn't be worth it to try and make a type system in the first place.

Community
  • 1
  • 1
Sitansu
  • 3,225
  • 8
  • 34
  • 61
0

If you're coming from JavaScript and learning Java, there's one huge difference that I don't think anyone else has mentioned yet.

In JavaScript, a function is an object. An object can contain properties whose values can be function objects, or they can be other objects or primitive values. When you call obj.method(arguments), the semantics are to look for a method property of the object (or a prototype), and if it's a function object, to call it.

In Java and other compiled languages with OOP features, functions are not objects. If you create a new object of a particular type, the "type" information in the object refers to a list of polymorphic functions for that type. That's how polymorphism works in these languages. If you call obj.method(arguments), and method is a method that can be overridden for derived types, then the program looks up obj's type, and then looks in the function list for the type to determine which function to call. What this means, to me, is that the object's type is a key piece of information in these languages, and OOP revolves around it; while in JavaScript, the "type" of an object is quite a bit less important, since the function itself is looked up by name in the object. One can use JavaScript in a way to make it look like it's emulating the way other languages handle OOP, but it's not required, and it's not a built-in part of the language.

I think you have to keep that in mind when going from one to the other. If you try too hard to relate Java features to JavaScript features you're already familiar with, it will be confusing.

ajb
  • 31,309
  • 3
  • 58
  • 84
0

If you are new to Java, I suggest you go to amazon.com and look for a beginning book on java that has good reviews and read it cover to cover. I think this will be the best use of your time rather than reading varoius articles on Java and picking it up piece-meal. I also suggest you don't try to port your knowledge of Javascript to Java, least you make learning Java that much harder. Note there are many books you can read for Java (web) development: Java, products built on Java (JSF, Tomcat, etc), and supporting technologies (HTML, CSS, XML, etc).

user2810910
  • 279
  • 1
  • 2