51
loading...
This website collects cookies to deliver better user experience
class Parent {
function sayHi() {
console.log("sayHi from Parent");
}
}
class Child {
function sayHi() {
console.log("sayHi from the Child");
}
}
class Window {
height: number;
width: number;
open: boolean;
toggleOpen() {}
area(){}
}
area
method. It calculates the area.Wall
. The code for the wall might look as follow.class Wall {
height: number;
width: number;
color: string;
area() {}
}
Rectangle
which has common properties from the both of the classes.class Rectangle {
height: number;
width: number;
area() {}
}
class Wall extends Rectangle {
color: string;
}
class Window extends Rectangle {
open: boolean;
toggleOpen() {}
}
Wall
is rectangle and Window
is also a rectangle, now let's suppose the we want to introduce new type of window called circle window. Rectangle
window because it does not make sense. Now this is a type of Window so you might be tempted to inherit from the window class but window class itself inherits from Rectangle
so although it can be extended but it will also inherit the methods from Rectangle
class which does not make sense and it as it is derived from Rectangle
.You wanted a banana but what you got was a gorilla holding the banana -- Joe Armstrong
class Circle {
radius: number;
area(){}
}
class CircleWindow extends Circle {
open: boolean;
toggleOpen() {}
}
Object composition is an alternative to class inheritance. Here, new functionality is obtained by assembling or composing objects to get more complex functionality. [...] Object composition is defined dynamically at run-time through objects acquiring references to other objects
interface Shape {
area(): number;
}
class Wall {
color: string;
area(){}
dimensions: Shape;
}
class Window {
open: boolean;
toggleOpen(){}
area(){}
dimensions: Shape;
}
class Rectangle implements Shape {
height;
width;
area(){}
}
class Circle implements Shape {
radius;
area(){}
}
Wall
and Window
we have a member variable called dimensions, which is of type Shape. The interface Shape
guarantees us that their will be always a method named area
present. So now we both Window
and Wall
can accept any object for its memember variable dimensions as long it satisfies the interface Shape
, in a way we have delegated the responsibility for calculating the area to the class Rectangle
and Circle
Abstract
class and Interface
. These are features provided by programming languages to aid in inheritance and composition.Abstract
classes are classes for which you create an instance of them. They maybe used to provide a default implementation in case the overriding classes does not provide one. In javacript you can create an abstract class by adding the keyword abstract
infront of the class.abstract class Destroyer {
onDestroy() {
db.close();
analytics.send("Database Closed);
}
}
class MyDestroyer extends Destroyer {
}
The class MyDestroyer gets the implemntation defined in class Destroyer.
Interface
are used to define set of properties that must exist on class. Interface only contains what a class implentating that interface should have it does not dictate what it should do. Interface are loosely coupled compared to Abstract classes and are usually favoured.abstract
and interface
. stay tuned!🙂