DEV Community

Discussion on: Is JS an OOP Language?

Collapse
 
jwp profile image
John Peters

For example, in classical OOP, we have to deal with interfaces
We only have to deal with interfaces when they are implemented as an entry to a function or method.
Consider this in C#

// Classes have implicit interface definitions
// plus the ability to create a new object of that type
public class Person
{
    // implicit interface properties of...
    string lastName;
    string firstName;
}
// if there's a method  anywhere in the project like this:

setName(Person person){
  // the parameter is a concrete Person object
  // that has implicit interfaces of lastName and firstName properties.
}

// but what about this?
setName(iPerson person){}

// and assume this:
// a definition of an interface
interface IPerson{
  string lastName;
  string firstName;
}
// here we have the same exact thing happening, 
// but the parameter is asking for an object of type interface
// and not a concrete person object.

// The only difference is that we cannot create a new instance of an interface. 
// To do that we'd need this.
// Change Person to implement the interface
class Person:IPerson{
  string lastName;
  string firstName;
}

//create a new person object which passes the interface test
var person = new Person()
// Works due to our Person Class
// implementation of iPerson
setName(iPerson person){}  

Enter fullscreen mode Exit fullscreen mode

Sound confusing? Why would we do this on just a person model class? Answer: We wouldn't, because the Person class with just 2 properties is sufficient and the interface is implicit.

There's two types of inheritance 'classical' which is vertical nature, it deals with classes, and subclasses. The interface (explicit or implicit) is 'horizontal in nature' and is seen as the ability to pass something in to be contained, it can be done via parameters or by object placeholders with the class or function. The concepts of composition use the horizontal style while classical inheritance is the vertical style.

It all boils down to one simple concept, can I morph my object in such a way that I can only see the properties or functions I want? If the answer is yes then we are using some form on inheritance/composition.

BTW, I saw that video years ago and disagreed with the general conclusions then.