The Syntax of Object-Oriented Programming Languages

Overview

Every language mentioned in this article uses the class keyword. C++, Java, and Node.js have similar syntax for extending Classes. The same is true for instancing new classes. Python does not use extends syntax but uses the parent class as the first argument.

Extending Classes

Java and Node both use the extends keyword, when extending classes. Python passes the class as the first argument, since the first argument of a function is always self. C++ uses the colon syntax as it’s extension keyword.

// C++
class TodoApp : public Todo {…}
// Java
public class TodoApp extends Todo {…}
// Node.js
class TodoApp extends Todo {…}
// Python
class TodoApp(Todo):

Instancing new classes

All the languages described use the class = new Class(args) syntax. C++ has multiple ways to instantiate a class, but the two most common have been provided. Python does not use the new keyword.

// C++
TodoApp todoapp(args);
TodoApp todoapp = new TodoApp(args);
// Java
TodoApp todoApp = new TodoApp(args);
// Node.js
let todoApp = new TodoApp(args);
// Python
todo_app = TodoApp(args)

Protecting methods and variables

Java and C++ both have public and private declaration modifiers. Python and Node.js are not strict about public and private attributes, but have work language specific ways of achieving it.

// C++
class TodoApp {
private:
int privateValue
void privateFunction(){...}
public:
void publicFunction(){...}
}// Java
public class TodoApp extends Todo {
public static function {...}
private function {...}
}
// Node.js
// Declare a function that is not a method of a Class
// Then call that function from the Class method
function __privateFunction(){...}class TodoApp {
publicFunction(){
__privateFunction()
}
}
// Python
Class Todo():
___private_method(self):
...
public_method(self):
...

Examples

Provided is the same application coded in multiple languages.

C++

Java

Python

Example Object Oriented Applications

The following is a interactive Node.js Todo application, written for your terminal.

--

--

--

I am working toward a simpler, more meaningful world. kristofferhebert.com

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kristoffer Hebert

Kristoffer Hebert

I am working toward a simpler, more meaningful world. kristofferhebert.com

More from Medium

The Reference Trap

In C++, write a method that remove a character from a C string using pointers

7 Steps to Become an Outstanding Java Developer

Best coding practices every java developer should follow