WAP to Create a Parent Class Named Shape with Data Member shapeName and Method dispShape() to Show the
shapeName.
Create a Child Class Named RealShape with Method calArea(int x, int y)
Create a Separate Test Class to Create an Object of Child Class and Invoke Methods from Parent and Child Classes.
// Parent class
class Shape {
String shapeName;
// Constructor to initialize shape name
Shape(String name) {
this.shapeName = name;
// Method to display shape name
void dispShape() {
System.out.println("Shape Name: " + shapeName);
// Child class
class RealShape extends Shape {
// Constructor to call parent constructor
RealShape(String name) {
super(name);
// Method to calculate area (as example: x * y)
void calArea(int x, int y) {
int area = x * y;
System.out.println("Calculated Area: " + area);
// Test class
public class ShapeTest {
public static void main(String[] args) {
// Create object of child class
RealShape obj = new RealShape("Rectangle");
// Call parent class method
obj.dispShape();
// Call child class method
obj.calArea(5, 10);
2. Write a programme to create a parent class student, write the data members, institute name, depart --ment, Roll No, and method
void accept() to take the values of data members and void display() to display the value of data members. 11) Create a child class ECE
student and define variable debt and method set dept() (111) Create another child class named CSE-student and write method as ECE
student child class.
Ans: import java.util.Scanner;
// Parent Class
class Student {
String instituteName;
String department;
String rollNo;
Scanner sc = new Scanner(System.in);
void accept() {
System.out.print("Enter Institute Name: ");
instituteName = sc.nextLine();
// This will be overridden by child class method setDept()
System.out.print("Enter Department (will be overwritten): ");
department = sc.nextLine();
System.out.print("Enter Roll No: ");
rollNo = sc.nextLine();
void display() {
System.out.println("\nStudent Details:");
System.out.println("Institute Name: " + instituteName);
System.out.println("Department: " + department);
System.out.println("Roll No: " + rollNo);
}
// Child Class for ECE Student
class ECEStudent extends Student {
String dept;
void setDept() {
dept = "ECE";
department = dept;
// Child Class for CSE Student
class CSEStudent extends Student {
String dept;
void setDept() {
dept = "CSE";
department = dept;
// Main class to test
public class StudentTest {
public static void main(String[] args) {
// ECE Student
System.out.println("Creating ECE Student:");
ECEStudent ece = new ECEStudent();
ece.setDept();
ece.accept();
ece.display();
// CSE Student
System.out.println("\nCreating CSE Student:");
CSEStudent cse = new CSEStudent();
cse.setDept();
cse.accept();
cse.display();
}
3. Write a JAVA Program using abstract class and demonstrate behaviour of abstract class. Write a JAVA Program to demonstrate
multiple inheritance implementation and method overloading
// Abstract class demonstration
abstract class Animal {
// Abstract method (no body)
abstract void sound();
// Concrete method
void breathe() {
System.out.println("All animals breathe.");
// Dog extends Animal and provides implementation for sound()
class Dog extends Animal {
void sound() {
System.out.println("Dog barks.");
// Main class
public class AbstractDemo {
public static void main(String[] args) {
// Animal a = new Animal(); // Not allowed, abstract class cannot be instantiated
Dog d = new Dog();
d.breathe(); // inherited method
d.sound(); // overridden abstract method
-----// Interface A
interface Printable {
void print();
}
// Interface B
interface Showable {
void show();
// Class implements both interfaces (Multiple Inheritance via Interfaces)
class Document implements Printable, Showable {
public void print() {
System.out.println("Printing document...");
public void show() {
System.out.println("Showing document...");
// Method Overloading
void print(String title) {
System.out.println("Printing document titled: " + title);
// Main class
public class MultipleInheritanceDemo {
public static void main(String[] args) {
Document doc = new Document();
doc.print(); // Method from Printable
doc.show(); // Method from Showable
doc.print("My Report"); // Overloaded method
Java Program: Exception Handling
java
CopyEdit
import java.util.Scanner;
public class ExceptionHandlingDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
System.out.print("Enter numerator: ");
int numerator = sc.nextInt();
System.out.print("Enter denominator: ");
int denominator = sc.nextInt();
// This may cause ArithmeticException
int result = numerator / denominator;
System.out.println("Result = " + result);
catch (ArithmeticException e) {
System.out.println("Error: Cannot divide by zero.");
catch (Exception e) {
System.out.println("An unexpected error occurred: " + e.getMessage());
finally {
System.out.println("This block always executes (e.g., closing resources).");
sc.close();
System.out.println("Program continues after exception handling...");