Close
Close

Create class named as ‘Parent’ and create a sub class ‘Child’ which extends from class ‘Parent’. Use these classes in ‘Transaction’ class.

   AJZIN


Answers

  •    Aakhya Singh

    The requested program is given below.


    // superclass
    class Parent {
    	public void parentMethod() {
    		System.out.println("This is method of Parent class");
    	}
    }
    
    // subclass
    class Child extends Parent {
    	public void childMethod() {
    		System.out.println("This is method of Child class");
    	}
    }
    // Transaction class
    class Transaction {
    	public static void main (String[] args) {
    		// creating object of Parent
    		Parent p = new Parent();
    		// creating object of Child
    		Child c = new Child();
    		
    		// object of Parent calling method of Parent
    		p.parentMethod();
    		// object of Child calling method of Child
    		c.childMethod();
    	}
    }
    

    Output

    This is method of Parent class

    This is method of Child class


    Here, we created a class named Parent having a method named parentMethod. We also created a class named Child having a method named childMethod, which extends the Parent class.


    Inside the Transaction class, we created an object p of Parent and c of Child. Finally, the object p called the parentMethod() method of Parent and c called the childMethod() method of Child.




Ask Yours
Post Yours
Write your answer