JavaScript Lesson 26 – Inheritance in OOP

Tram Ho

Excerpt from learn.adacore.com ‘s OOP:

Object-oriented programming (OOP) is a large and ill-defined concept in programming languages ​​and one that varies to encompass many different languages ​​because languages ​​often implement their own vision of it, with similarities and differences from the other implementations.

However, one model mostly “won” the battle of what object-oriented means, if only by sheer popularity. It’s the model used in the Java programming language, which is very similar to the one used by C++.

And after a copy/paste via Google Translate:

Object-oriented programming (OOP) is a large and difficult concept to define in programming languages, and also a concept that tends to have different meanings because different languages ​​often implement their own vision of it.

However, there is one model that is almost “winning” the battle over the meaning of object-oriented, if only in terms of popularity. It is the pattern used in the Java programming language, which has a lot in common with the model used by C++ .

Great. I really don’t know how to define the scope of discussion, so fortunately I got this excerpt from the engineers who created the Ada language. So we can take Java as a reference point and find a way to represent the equivalent logic in JavaScript .

Online REPL

Regarding the test run of the example code, we can use the free Online tools provided by replit.com and do not need to install any additional tools on the computer in use.

Online Java REPL: https://replit.com/languages/java10

Ah.. and possibly Python too. This language is very popular and is considered the JavaScript of languages ​​that do not use the Block Style syntax, encapsulating blocks of code with curly braces { ... } ; Instead, the statements in Python will be identified as the same block if there is the same indentation at the beginning of the Indentation Style line.

Online Python REPL: https://replit.com/languages/python3

Please try clicking the REPL links to test the program "Hello, World!" of each language. Here, we will talk a little bit about "Hello, World!" of Java to quickly omit some elements that do not need to be detailed.

The starting point of a Java program is a static main method of any class . However, here we will use a separate class Main as the starting point and will create other class to describe the logic to learn in OOP .

For the time being, we will only need to care about where the statements begin to be executed, which is the line String name = "Java"; and all other statements in the { ... } block of the main method. And the method System.out.println ("a string"); is the only command that we need to memorize to print a String to the console on the right side.

There is one fundamental difference that we need to note in terms of the syntax of Java from JavaScriptJava is a static-typing language and identifiers when generated need to be declared. with the data type name right in front. Specifically in the statement String name = "Java"; then the variable name is typed to be used to store any String . If we intentionally create logic that assigns an arithmetic value to the name , when we press the Run button to run the program, we will see an error message.

The basic type-hint in Java include: String , int , float , boolean , and void are special cases used to mark methods that return no value.

Ok.. so that’s okay. Let’s start talking about Inheritance .

Inheritance

Technically, Inheritance and Extension is a feature that helps us to reduce the amount of logic code that has to be rewritten many times when designing class that have a lot in common.

The first example of Inheritance and Extension in Java is not much different from what we already know in JavaScript. Here we have class Universe defined as Inheriting and Extending from class Entity .

Ah.. there is another small note that the constructor in JavaScript has the default name of constructor , then through Java we will see using the name of the class instead.

So we will temporarily conclude, Inheritance and Extension is the code implementation logic so that a new class template can borrow property and method elements from an existing class template, if allowed. .

The paragraph nếu được cho phép at the end we will talk about later, because the elements Inheritance , Polymorphism , Abstraction , and Encapsulation , are also somewhat interrelated.

Another note is that all class in the Java environment are implicitly inherited directly from the existing class Object if we do not specify. So does JavaScript . Therefore, in addition to the property and method elements that we write our own definition of, each object will also have many other property and method that provide utilities that can be very useful in some cases.

Multiple Inheritance

In terms of reflecting things in our lives, Multiple Inheritance is the most natural pattern that helps us form a class with trait traits borrowed from various sources.

Obviously, each of us will inherit only some traits from our fathers, some features from our mothers, and some other features from the people and environment around us, in addition to those belonging to our parents. spiritual stimulation existed prior to the reception of the present body.

It is for this reason that the Single Inheritance Inheritance of Java or JavaScript for short is not ideal for reflecting, representing, or reimagining an aspect of life in the environment. program.

That is also the reason many other programming languages ​​also support Multiple Inheritance form. In this case, a new class can be extends to many previously defined class at the same time. And here we will look at an example of Python .

In this example, the class Nihonjin is created by collecting Trait traits from various sources and the stamina factor is combined when appearing in more than one Trait . In a similar way, we can create class Vietnamese with certain Trait that have some overlap with some Trait that the class Nihonjin also uses.

At the syntactic level of the language, some other languages ​​like PHP or Scala also have the trait keyword separate from the class and have a feature against creating object directly from trait templates. The concept of trait or trait , also known as a mixin , was created to describe an aspect used when creating a class template. And often a class will be composed from a previously defined class and the necessary trait .

Trait/Mixin in JavaScript

To express the logic that works similarly to the concept of trait or mixin as defined above, in the JavaScript environment we need to understand a little more about the template generated by the class .

In fact, when we use the class Entity syntax to define a template, an object representation is created and appended to the Entity identifier name. Also the prototype object will be attached to the prototype property of Entity .

When we create an object from the class Entity by implementing new , the property in the prototype defined using the normal syntax of the class will be copy to the entity object . Ah.. and also the prototype of the class that precede the class Entity in the inheritance line.

However, since we do not define class Entity inheriting from any other class and so the above definition is equivalent to class Entity extends Object { ... } . However, the property of Object.prototype are configured to not be enumerable (which can be counted in collection operations), so we won’t have any more properties than the definition of the class Entity . .

The next part of the story is that we created literal object to describe the Trait and copied the property of these Trait to the prototype of the class Entity before doing any other operations in the constructor. constructor . Thus, the newly created object will default to the elements defined by the Trait as above.

The operation of copying Trait to the prototype of a class only needs to be done once, and does not need to be repeated every time the constructor is used by new . However, the Object.assign method will automatically overwrite existing property , so we don’t need to pay any more attention.

(unpublished) [JavaScript] Lesson 27 – Encapsulation in OOP

Share the news now

Source : Viblo