xxxxxxxxxx
// parent class
class Person {
constructor(name) {
this.name = name;
this.occupation = "unemployed";
}
greet() {
console.log(`Hello ${this.name}.`);
}
}
// inheriting parent class
class Student extends Person {
constructor(name) {
// call the super class constructor and pass in the name parameter
super(name);
// Overriding an occupation property
this.occupation = 'Student';
}
// overriding Person's method
greet() {
console.log(`Hello student ${this.name}.`);
console.log('occupation: ' + this.occupation);
}
}
let p = new Student('Jack');
p.greet();
xxxxxxxxxx
import java.io.*;
class Animal {
void eat()
{
System.out.println("eat() method of base class");
System.out.println("eating.");
}
}
class Dog extends Animal {
void eat()
{
System.out.println("eat() method of derived class");
System.out.println("Dog is eating.");
}
}
class MethodOverridingEx {
public static void main(String args[])
{
Dog d1 = new Dog();
Animal a1 = new Animal();
d1.eat();
a1.eat();
Animal animal = new Dog();
// eat() method of animal class is overridden by
// base class eat()
animal.eat();
}
}