// Class implementation with different methods
class MyClass {
// Method with no return type and with parameter-list
void displayMessage(String message) {
System.out.println("Message: " + message);
}
// Method with return type and without parameter-list
String getGreeting() {
return "Hello, World!";
}
// Method with return type and with parameter-list
int addNumbers(int a, int b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
// No return type and with parameter-list
obj.displayMessage("Welcome to Java!");
// Return type and without parameter-list
System.out.println(obj.getGreeting());
// Return type and with parameter-list
System.out.println("Sum: " + obj.addNumbers(10, 20));
}
}
Aim: To write a Java program that implements method overloading.
class MathOperation {
// Method for adding two integers
int add(int a, int b) {
return a + b;
}
// Method for adding three integers
int add(int a, int b, int c) {
return a + b + c;
}
// Method for adding two doubles
double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
MathOperation obj = new MathOperation();
System.out.println("Sum of 2 numbers: " + obj.add(10, 20));
System.out.println("Sum of 3 numbers: " + obj.add(10, 20, 30));
System.out.println("Sum of 2 doubles: " + obj.add(10.5, 20.5));
}
}
Aim: To write a Java program to implement constructor.
class Person {
String name;
int age;
// Constructor
Person(String name, int age) {
this.name = name;
this.age = age;
}
void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
// Creating an object using constructor
Person p = new Person("Alice", 25);
p.displayInfo();
}
}
Aim: To write a Java program to implement constructor overloading.
class Rectangle {
int length;
int width;
// Constructor with no parameters
Rectangle() {
length = 5;
width = 5;
}
// Constructor with parameters
Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
void displayArea() {
System.out.println("Area: " + (length * width));
}
}
public class Main {
public static void main(String[] args) {
Rectangle rect1 = new Rectangle();
Rectangle rect2 = new Rectangle(10, 20);
rect1.displayArea();
rect2.displayArea();
}
}
Aim: To write a Java program to implement Single Inheritance.
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.makeSound(); // Inherited method
}
}
class A
{
A()
{
System.out.println("Inside A's Constructor");
}
}
class B extends A
{
B()
{
System.out.println("Inside B's Constructor");
}
}
class C extends B
{
C()
{
System.out.println("Inside C's Constructor");
}
}
class multidemo
{
public static void main(String args[])
{
C c1=new C();
}
}
Output:
Inside A's Constructor
Inside B's Constructor
Inside C's Constructor
Aim: To write a Java program for an abstract class to find areas of different shapes.
abstract class Shape {
abstract void area();
}
class Circle extends Shape {
double radius;
Circle(double radius) {
this.radius = radius;
}
void area() {
System.out.println("Area of Circle: " + (Math.PI * radius * radius));
}
}
class Rectangle extends Shape {
double length, width;
Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
void area() {
System.out.println("Area of Rectangle: " + (length * width));
}
}
public class Main {
public static void main(String[] args) {
Shape s1 = new Circle(7);
s1.area();
Shape s2 = new Rectangle(5, 10);
s2.area();
}
}
Aim: To write a Java program as an example for the “super” keyword.
class Animal {
void display() {
System.out.println("Animal is walking");
}
}
class Dog extends Animal {
void display() {
super.display(); // Calls the superclass method
System.out.println("Dog is barking");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.display();
}
}
Aim: To write a Java program to implement Interface.
(i) First form of Interface Implementation
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.sound();
}
}
interface Vehicle {
void drive();
}
class Car implements Vehicle {
public void drive() {
System.out.println("Car is driving");
}
}
public class Main {
public static void main(String[] args) {
Vehicle vehicle = new Car();
vehicle.drive();
}
}
interface Printer {
void print();
}
class InkjetPrinter implements Printer {
public void print() {
System.out.println("Printing from Inkjet Printer");
}
}
public class Main {
public static void main(String[] args) {
Printer printer = new InkjetPrinter();
printer.print();
}
}
(iv) Fourth form of Interface Implementation
interface Shape {
void draw();
}
class Circle implements Shape {
public void draw() {
System.out.println("Drawing a Circle");
}
}
public class Main {
public static void main(String[] args) {
Shape shape = new Circle();
shape.draw();
}
}
Aim: To write a Java program that implements Runtime polymorphism.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myDog = new Dog();
Animal myCat = new Cat();
myAnimal.sound(); // Animal sound
myDog.sound(); // Dog barks (Runtime polymorphism)
myCat.sound(); // Cat meows (Runtime polymorphism)
}
}