TypeScript - Inheritance
Typescript - Inheritance:
* Inheritance is the OOP Cocept.
* Chile class can extends the parent class object and methods.
* Support Abstract classess and overriding.
* It supports only single inheritance.
* But you can implement multiple interfaces.
Eg:
Shape.ts
export class Shape {
constructor(private _x: number, private _y: number) {
}
public get x(): number {
return this._x;
}
public set x(value: number) {
this._x = value;
}
public get y(): number {
return this._y;
}
public set y(value: number) {
this._y = value;
}
getInfo(): string {
return `x=${this._x}, y=${this._y}`;
}
}
Circle.ts
import { Shape } from './Shape';
export class Circle extends Shape {
constructor(theX: number, theY: number,
private _radius: number) {
super(theX, theY);
}
public get radius(): number {
return this._radius;
}
public set radius(value: number) {
this._radius = value;
}
getInfo(): string {
return super.getInfo() + `, radius=${this._radius}`;
}
}
Driver.ts
import { Shape } from './Shape';
import { Circle } from './Circle';
let myShape = new Shape(10, 15);
console.log(myShape.getInfo());
let myCircle = new Circle(5, 10, 20);
console.log(myCircle.getInfo());
* Inheritance is the OOP Cocept.
* Chile class can extends the parent class object and methods.
* Support Abstract classess and overriding.
* It supports only single inheritance.
* But you can implement multiple interfaces.
Eg:
Shape.ts
export class Shape {
constructor(private _x: number, private _y: number) {
}
public get x(): number {
return this._x;
}
public set x(value: number) {
this._x = value;
}
public get y(): number {
return this._y;
}
public set y(value: number) {
this._y = value;
}
getInfo(): string {
return `x=${this._x}, y=${this._y}`;
}
}
Circle.ts
import { Shape } from './Shape';
export class Circle extends Shape {
constructor(theX: number, theY: number,
private _radius: number) {
super(theX, theY);
}
public get radius(): number {
return this._radius;
}
public set radius(value: number) {
this._radius = value;
}
getInfo(): string {
return super.getInfo() + `, radius=${this._radius}`;
}
}
Driver.ts
import { Shape } from './Shape';
import { Circle } from './Circle';
let myShape = new Shape(10, 15);
console.log(myShape.getInfo());
let myCircle = new Circle(5, 10, 20);
console.log(myCircle.getInfo());
Comments
Post a Comment