1class Animal { private name: string; constructor(theName: string) { this.name = theName; }}
2class Rhino extends Animal { constructor() { super("Rhino"); }}
3class Employee { private name: string; constructor(theName: string) { this.name = theName; }}
4let animal = new Animal("Goat");let rhino = new Rhino();let employee = new Employee("Bob");
5animal = rhino;animal = employee;Type 'Employee' is not assignable to type 'Animal'.
6 Types have separate declarations of a private property 'name'.2322Type 'Employee' is not assignable to type 'Animal'.
7 Types have separate declarations of a private property 'name'.Try
1class Info {
2 private name: string ;
3 constructor(n:string){
4 this.name = n ;
5 };
6 describe(){
7 console.log(`Your name is ${this.name}`);
8 }
9}
10
11const a = new Info('joyous');
12a.describe();
1class Animal {
2 move(distanceInMeters: number = 0) {
3 console.log(`Animal moved ${distanceInMeters}m.`);
4 }
5}
6
7class Dog extends Animal {
8 bark() {
9 console.log("Woof! Woof!");
10 }
11}
12
13const dog = new Dog();
14dog.bark();
15dog.move(10);
16dog.bark();
1// Parent class
2class Info {
3 protected name: string ;
4 constructor(n:string){
5 this.name = n ;
6 };
7 describe(){
8 console.log(`Your name is ${this.name}`);
9 }
10}
11
12//inherited class (you can overwrite methods of parent class, super is used to
13// connect to the parent parameter(s) . )
14
15class Detail extends Info{
16 constructor(name:string, public age:number){
17 super(name);
18 }
19 findAge(){
20 console.log(`${this.name} age is ${this.age}`)
21 }
22}
23
24const b = new Detail('jank', 23);
25b.describe();
26b.findAge();