swati-jagtap / javascript-practice

0 stars 0 forks source link

15.1.2--Create a Dog and Cat child class from the Animal Class. 15.2.1--Override the method you create in Animal class #153

Open swati-jagtap opened 3 years ago

swati-jagtap commented 3 years ago
 class Animal{
     constructor(name,age,color,legs){
         this.name=name;
         this.age=age;
         this.color=color;
         this.legs=legs;

         console.log(this)  //1

     }

   get getinfo(){
       return `${this.name.toUpperCase()} is my pet.${this.name} is ${this.age} yers old.`;
   }
   setName(){
       this.owner="johny";
     // let Name=this.name.toUpperCase();
       return `${this.owner} was his owner`;
      // return `${Name}`;
   }
   birthYear(){
     const d=new Date();
     const Birth=d.getFullYear()-this.age;
     return `${this.name}'s Year Of Birth is : ${Birth}`;
   }
 }
 const A1=new Animal("tyson",2,"brown",4);
 console.log(A1.getinfo);
 console.log(A1.setName());
 console.log(A1.birthYear());
 console.log(A1);      //2
 console.log(Animal)     //3
 console.log(A1.name);      //4

 //15.1.2--Create a Dog and Cat child class from the Animal Class.
 //15.2.1--Override the method you create in Animal class
 class Dog extends Animal{

  birthYear(){             //method overidding
    const d=new Date();
    const Birth=d.getFullYear()-this.age;
    return `${this.name}'s Year Of Birth is : ${Birth}`;
  }
 }
 class Cat extends Animal{
   constructor(name,age,color,legs,gender){
     super(name,age,color,legs)               // to access  parent in child we need to use super() first
     this.gender=gender;
     console.log(this); 
   }

}
 const d1=new Dog("Cherry",1,"white",4);
 console.log(d1.birthYear());
 const c1=new Cat("Mauu",1,"white",4,"FEmale");
 //console.log(d1);

image