Day 11 - 25 August, 2024

Program 31 : WAP to create a program to understand the concept of MultiLevel Inheritance

class A { public void showA() { System.out.println("This message from class A"); } } class B extends A { public void showB() { System.out.println("This message from class B"); } } class C extends B { public void showC() { System.out.println("This message from class C"); } } class P31 { public static void main(String[] args) { A ob1 = new A(); ob1.showA(); System.out.println("-----------------------------------"); B ob2 = new B(); ob2.showA(); ob2.showB(); System.out.println("-----------------------------------"); C ob3 = new C(); ob3.showA(); ob3.showB(); ob3.showC(); } } Copy Code
Expected Output:
This message from class A
-----------------------------------
This message from class A
This message from class B
-----------------------------------
This message from class A
This message from class B
This message from class C

Program 32 : WAP to create a program to understand the concept of Method Overloading

import java.util.*; class Figure{ public int area(int s) { return s*s; } public int area(int l, int b) { return l*b; } } class P32 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int s, l, b, a1, a2; System.out.print("Enter the side of square : "); s = sc.nextInt(); System.out.println("-----------------------------------"); Figure sq = new Figure(); a1 = sq.area(s); System.out.println("Area of Square : " + a1); System.out.println("-----------------------------------"); System.out.print("Enter the length of rectangle : "); l = sc.nextInt(); System.out.print("Enter the breadth of rectangle : "); b = sc.nextInt(); System.out.println("-----------------------------------"); Figure ra = new Figure(); a2 = ra.area(l, b); System.out.println("Area of Rectangle : " + a2); System.out.println("-----------------------------------"); } } Copy Code
Expected Output:
Enter the side of square : 4
-----------------------------------
Area of Square : 16
-----------------------------------
Enter the length of rectangle : 5
Enter the breadth of rectangle : 3
-----------------------------------
Area of Rectangle : 15
-----------------------------------

Program 33 : WAP to create a program to understand the concept of Method Overriding

import java.util.*; class X { public void m1() { System.out.println("Class X's m1 Method Called!!"); } public void m2() { System.out.println("Class X's m2 Method Called!!"); } } class Y extends X { public void m1() { System.out.println("Class Y's m1 Method Called!!"); } public void m3() { System.out.println("Class Y's m3 Method Called!!"); } } class P33 { public static void main(String[] args) { System.out.println("-----------------------------------"); X ob1 = new X(); ob1.m1(); ob1.m2(); System.out.println("-----------------------------------"); Y ob2 = new Y(); ob2.m1(); ob2.m2(); ob2.m3(); System.out.println("-----------------------------------"); } } Copy Code
Expected Output:
-----------------------------------
Class X's m1 Method Called!!
Class X's m2 Method Called!!
-----------------------------------
Class Y's m1 Method Called!!
Class X's m2 Method Called!!
Class Y's m3 Method Called!!
-----------------------------------