Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

LESSON 3.

1: JAVA METHOD
OVERRIDING
P ELEC 1 - OBJECT ORIENTED PROGRAMMING
METHOD OVERRIDING
• Inheritance is an OOP property that allows us to derive a new class (subclass)
from an existing class (superclass). The subclass inherits the attributes and
methods of the superclass.

• If the same method is defined in both the superclass and the subclass, then
the method of the subclass class overrides the method of the superclass. This
is known as method overriding.

P ELEC 1 - OBJECT ORIENTED PROGRAMMING


public class Parent {
void disp() {
System.out.println("This is from the Parent
method");
}
}
class Child extends Parent {
void disp() {
super.disp();
System.out.println("This is from the Child
method");

}
}
class TestSiper {
public static void main(String[]args) {
Child ch = new Child();
ch.disp();
}
}
P ELEC 1 - OBJECT ORIENTED PROGRAMMING
public class Parent1 {
int age;

Parent1(int age) {
this.age = age;
}
void getAge() {
System.out.println("The value of age is" +
age);
}
}
class Child1 extends Parent1 {
Child1(int age) {
super(age);
}
}
class testage {
public static void main(String[]args) {
Child1 ch1 = new Child1(20);
ch1.getAge();
}
P ELEC 1 - OBJECT ORIENTED PROGRAMMING
}
public class Animal
{
void eat() {
System.out.println("eating…");
}
}
class Dog extends Animal {
void eat() {
super.eat();
System.out.println("I ate bones…");
}
void bark() {
System.out.println("barking…");
}
}
class TestInheritance{
public static void main(String args[]){

Dog d = new Dog();


d.eat();
d.bark();

}
}
P ELEC 1 - OBJECT ORIENTED PROGRAMMING
That ends our
Lesson 3.1

Thank you.

P ELEC 1 - OBJECT ORIENTED PROGRAMMING

You might also like