It looks like you're using an older browser that is unsupported. To get the best experience, we recommend you

upgrade your browser

What's OOP in Object-Oriented-Programming?

Objection-Oriented Programming (OOP) is a programming language model that simplifies and organizes your code into self-contained objects or entities, to optimize your capability of reusable and dry-code. Applicable to a majority of programming languages, OOP not only makes your code smarter, it also helps aid you into becoming a better software engineer, by enhancing your ability to orientate large processes into finite pieces.

A Square is a Rectangle, but a Rectangle is not a Square

Oh Geometry...this riddle is one of the first that young math students will come across in entry-level Geometry courses, but what does it mean? Let’s examine the properties of a square:

  • Four sides of equal length
  • Four 90° angles

The properties of a rectangle are near identical:

  • Four sides
  • Four 90° angles

By definition, a square meets all of the requirements of a rectangle, while a rectangle cannot meet the equal sides of length requirement of the square. Let’s see how this applies in OOP.

By understanding that a square can derive from a rectangle, we understand that a square extends a rectangle. Utilizing TypeScript, we can see exactly how this relationship works with classes.

export class Rectangle {

sides: number;

constructor(public width: number, public height: number) {

this.sides = 4;

}

}

export class Square extends Rectangle {

constructor(public width: number, public height: number) {

super(5, 5);

console.log('width: ' + this.width + ', height: ' + this.height + ', sides: '+ this.sides); // outputs: width: 5, height: 5, sides: 4

}

}

As we can see, the Square class extends its parent Rectangle class, inheriting all of it’s properties, including: width, height and the number of sides.

Though an elementary example of the root-basics of OOP, we can see the immediate benefit of class extension. Including:

  • Removing duplicate declaration of reusable variables
  • Shorthand automation (the parent constructor automatically sets the number of sides on the square)

If you are interested in learning more about OOP, classes, interfaces, TypeScript and an extension of other programming resources, subscribe to our blog to get automatically notified as new articles are published.

This post is the first part of a multi-step series, taking you from to 0 to 100 in Object-Oriented Programming in TypeScript.

Sydney Nordquist

Not to mention refreshing, positive and downright invigorating. Should we expect anything less from a personality that absolutely bubbles…

 Read more.

We'll have you at hello.

Thanks, !

We’ve got your message and we’ll connect with you shortly.

Dismiss