Professional Documents
Culture Documents
Lesson 3.1: Java Method: Overriding
Lesson 3.1: Java Method: Overriding
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.
}
}
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[]){
}
}
P ELEC 1 - OBJECT ORIENTED PROGRAMMING
That ends our
Lesson 3.1
Thank you.