Today, I learnt Inheritance & Method-Overriding and made a mini-task.
Inheritance - Child class can use Parent class method using keyword extends, instead of creating it again in Child class.
Method-Overriding - Instead of using the same implementation/logic of Parent class method, Child class can override it's own implementation but with same method name, same number of arguments, same type of arguments and same return type.
Parent.java
public class Parent{
public void parent_method(){
System.out.println("I'm Parent Method'");
}
}
Child.java
public class Child extends Parent{
public static void main(String[] args){
Child c = new Child();
c.parent_method(); //checked for Inheritance concept
c.parent_method(); //called for Method-Overriding
}
@Override
public void parent_method(){
System.out.println("I'm new Child method now'");
}
}
Main.java
public class Main{
public static void main(String[] args){
Child c = new Child();
Parent p = new Parent();
c.parent_method();
p.parent_method();
}
}
Output:
I'm new Child method now'
I'm Parent Method'
Top comments (0)