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) {
[Link] = name;
// Method to display shape name
void dispShape() {
[Link]("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;
[Link]("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
[Link]();
// Call child class method
[Link](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 [Link];
// Parent Class
class Student {
String instituteName;
String department;
String rollNo;
Scanner sc = new Scanner([Link]);
void accept() {
[Link]("Enter Institute Name: ");
instituteName = [Link]();
// This will be overridden by child class method setDept()
[Link]("Enter Department (will be overwritten): ");
department = [Link]();
[Link]("Enter Roll No: ");
rollNo = [Link]();
void display() {
[Link]("\nStudent Details:");
[Link]("Institute Name: " + instituteName);
[Link]("Department: " + department);
[Link]("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
[Link]("Creating ECE Student:");
ECEStudent ece = new ECEStudent();
[Link]();
[Link]();
[Link]();
// CSE Student
[Link]("\nCreating CSE Student:");
CSEStudent cse = new CSEStudent();
[Link]();
[Link]();
[Link]();
}
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() {
[Link]("All animals breathe.");
// Dog extends Animal and provides implementation for sound()
class Dog extends Animal {
void sound() {
[Link]("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();
[Link](); // inherited method
[Link](); // 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() {
[Link]("Printing document...");
public void show() {
[Link]("Showing document...");
// Method Overloading
void print(String title) {
[Link]("Printing document titled: " + title);
// Main class
public class MultipleInheritanceDemo {
public static void main(String[] args) {
Document doc = new Document();
[Link](); // Method from Printable
[Link](); // Method from Showable
[Link]("My Report"); // Overloaded method
Java Program: Exception Handling
java
CopyEdit
import [Link];
public class ExceptionHandlingDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
try {
[Link]("Enter numerator: ");
int numerator = [Link]();
[Link]("Enter denominator: ");
int denominator = [Link]();
// This may cause ArithmeticException
int result = numerator / denominator;
[Link]("Result = " + result);
catch (ArithmeticException e) {
[Link]("Error: Cannot divide by zero.");
catch (Exception e) {
[Link]("An unexpected error occurred: " + [Link]());
finally {
[Link]("This block always executes (e.g., closing resources).");
[Link]();
[Link]("Program continues after exception handling...");