(Using Inheritance)
Create a parent class named “Parent”, with a method that displays “I am a Parent Class”.
Also create a subclass that named “Child” that has a method that prints “I am a Child Class”.
Now create a class with Main Method, where you will:
Create an object for each class (1 for “Parent” and 1 for “Child”)
Call the method of “Child” class by using the Child object
Call the method of “Parent” class by object of the “Parent” class
package parent;
public class Parent {
public void print(){
System.out.println("I am a Parent Class");
}
public static void main(String[] args) {
Parent pa = new Parent();
Child ch = new Child();
pa.print();
ch.display();
}
}
class Child extends Parent{
public void display(){
System.out.println("I am a Child Class");
}
}
Comments
Leave a comment