3(a)
Aim:-
To Write a JAVA program to implement class mechanism and to Create a
class, methods and invoke them inside main method.
Algorithm:-
Step-1 :- Class Definition & Method Creation
Step-2 :-Main Method Setup
Step-3 :-Main Method Setup
Step-4 :-Invoke Methods
Step-5 :-Print the Result
Program:-
// Class with methods and main method to invoke them
class MyClass {
// Method 1
void greet() {
System.out.println("Hello, welcome to Java Programming!");
// Method 2
int addNumbers(int a, int b) {
return a + b;
// Main Method
public static void main(String[] args) {
// Create an object of MyClass
MyClass obj = new MyClass();
// Invoke methods
obj.greet();
int result = obj.addNumbers(5, 10);
System.out.println("Sum of numbers: " + result);
Output:-
Hello, welcome to Java Programming!
Sum of numbers: 15
Result:-
Thus the program has been executed sucessfully for the program to
implement class mechanism and to Create a class, methods and invoke them inside
main method.
b)
Aim:-
To Write a JAVA program to implement method overloading.
Algorithm:-
Step-1:-Class Definition & Method Overloading
Step-2:-Main Method Setup
Step-3:-Object Creation
Step-4:-Invoke Overloaded Methods
Step-5:-Print the Results
Program:-
// Class to demonstrate method overloading
class OverloadExample {
// Method 1: Adds two integers
int add(int a, int b) {
return a + b;
// Method 2: Adds three integers (method overloading)
int add(int a, int b, int c) {
return a + b + c;
// Main Method
public static void main(String[] args) {
OverloadExample obj = new OverloadExample();
// Invoke overloaded methods
int sum1 = obj.add(5, 10);
int sum2 = obj.add(3, 6, 9);
System.out.println("Sum of two numbers: " + sum1);
System.out.println("Sum of three numbers: " + sum2);
Output:-
Sum of two numbers: 15
Sum of three numbers: 18
Result:-
Thus, the program has been executed sucessfully for the program to
implement method overloading.
C)
Aim:-Write a JAVA program to implement constructor.
Algorithm:
Step-1:-Class Definition & Variable Declaration
Step-2:-Constructor Definition
Step-3:-Main Method Setup
Step-4:-Object Creation
Step-5:-Print the Constructor Message
Program:-
// Class with a constructor
class ConstructorExample {
int num;
// Constructor
ConstructorExample() {
num = 10; // Initializing variable
System.out.println("Constructor called. Value of num: " + num);
// Main Method
public static void main(String[] args) {
// Creating an object of ConstructorExample
ConstructorExample obj = new ConstructorExample();
}
}
Output:-
Constructor called. Value of num: 10
Result:-
Thus the program has been executed sucessfully for the program to implement
constructor.
D)
Aim:-Write a JAVA program to implement constructor overloading.
Algorithm:
Step-1:-Class Definition & Variable Declaration:
Step-2:-Define Overloaded Constructors
Step-3:-Main Method Setup
Step-4:-Invoke Different Constructors
Step-5:-Print Constructor Messages
Program:-
// Class to demonstrate constructor overloading
class ConstructorOverload {
int num;
String text;
// Default constructor
ConstructorOverload() {
num = 0;
text = "Default";
System.out.println("Default Constructor: num = " + num + ", text = " + text);
// Parameterized constructor with one parameter
ConstructorOverload(int n) {
num = n;
text = "Single parameter";
System.out.println("Single Parameter Constructor: num = " + num + ", text = " +
text);
}
// Parameterized constructor with two parameters
ConstructorOverload(int n, String t) {
num = n;
text = t;
System.out.println("Two Parameter Constructor: num = " + num + ", text = " +
text);
// Main Method
public static void main(String[] args) {
// Invoking different constructors
ConstructorOverload obj1 = new ConstructorOverload();
ConstructorOverload obj2 = new ConstructorOverload(5);
ConstructorOverload obj3 = new ConstructorOverload(10, "Overloaded");
Output:
Default Constructor: num = 0, text = Default
Single Parameter Constructor: num = 5, text = Single parameter
Two Parameter Constructor: num = 10, text = Overloaded
Result:-
Thus the program has been executed sucessfully for the program to implement
constructor overloading.
4(a)
Aim:-
To Write a JAVA program to implement Single Inheritance
Algorithm:-
Step-1:-Superclass Definition (Animal):
Step-2:-Subclass Definition (Dog):
Step-3:-Main Method Setup
Step-4:-Object Creation:
Step-5:-Invoke Superclass and Subclass Methods:
Program:-
// Superclass
class Animal {
void sound() {
System.out.println("This is an animal sound");
// Subclass
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
public class SingleInheritance {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // calling superclass method
dog.bark(); // calling subclass method
Output:-
This is an animal sound
Dog barks
Result:-
Thus the program has been executed sucessfully for the program to implement
Single Inheritance.
(b)
Aim:-
Write a JAVA program to implement multi level Inheritance
Algorithm:-
Step-1:-Base Class Definition (Animal):
Step-2:-Derived Class Definition (Dog):
Step-3:-Further Derived Class Definition (Puppy):
Step-4:-Main Method Setup:
Step-5:-Object Creation and Method Invocation:
Program:-
// Base Class
class Animal {
void sound() {
System.out.println("This is an animal sound");
// Derived Class
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
// Further Derived Class
class Puppy extends Dog {
void weep() {
System.out.println("Puppy weeps");
public class MultiLevelInheritance {
public static void main(String[] args) {
Puppy puppy = new Puppy();
puppy.sound(); // Calling method from Animal class
puppy.bark(); // Calling method from Dog class
puppy.weep(); // Calling method from Puppy class
Output:-
This is an animal sound
Dog barks
Puppy weeps
Result:-
Thus the program has been executed sucessfully for the program to
implement multi level Inheritance.
C)
Aim:-
Write a JAVA program for abstract class to find areas of different shapes
Algorithm:-
Step-1:-Abstract Class Definition (Shape)
Step-2:-Circle Class Definition
Step-3:-Rectangle Class Definition
Step-4:-Triangle Class Definition
Step-5:-Main Method and Object Creation:
Program:-
// Abstract class Shape
abstract class Shape {
abstract double area();
// Class for Circle
class Circle extends Shape {
double radius;
Circle(double radius) {
this.radius = radius;
@Override
double area() {
return Math.PI * radius * radius;
// Class for Rectangle
class Rectangle extends Shape {
double length, width;
Rectangle(double length, double width) {
this.length = length;
this.width = width;
@Override
double area() {
return length * width;
// Class for Triangle
class Triangle extends Shape {
double base, height;
Triangle(double base, double height) {
this.base = base;
this.height = height;
@Override
double area() {
return 0.5 * base * height;
public class AbstractClassShapes {
public static void main(String[] args) {
Shape circle = new Circle(5);
Shape rectangle = new Rectangle(4, 6);
Shape triangle = new Triangle(3, 7);
System.out.println("Area of Circle: " + circle.area());
System.out.println("Area of Rectangle: " + rectangle.area());
System.out.println("Area of Triangle: " + triangle.area());
Output:-
Area of Circle: 78.53981633974483
Area of Rectangle: 24.0
Area of Triangle: 10.5
Result:-
Thus the program has been executed sucessfully for the program for abstract
class to find areas of different shapes