0% found this document useful (0 votes)
14 views2 pages

Java Programs Terminal Ready

The document contains Java programs demonstrating class and object implementation, method overloading, and constructor overloading. It includes examples of a Student class, a Calculator class with overloaded add methods, and a Book class with multiple constructors. Each program showcases how to define classes, create objects, and utilize method and constructor overloading in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Java Programs Terminal Ready

The document contains Java programs demonstrating class and object implementation, method overloading, and constructor overloading. It includes examples of a Student class, a Calculator class with overloaded add methods, and a Book class with multiple constructors. Each program showcases how to define classes, create objects, and utilize method and constructor overloading in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Programs - Class, Method Overloading, Constructor Overloading

a) Program to implement Class and Object (ClassAndObject.java)

class Student {
String name;
int age;

void setDetails(String n, int a) {


name = n;
age = a;
}

void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}

public class ClassAndObject {


public static void main(String[] args) {
Student s1 = new Student();
s1.setDetails("Nikitha", 18);
s1.displayDetails();
}
}

b) Program to implement Method Overloading (MethodOverloading.java)

class Calculator {
int add(int a, int b) {
return a + b;
}

int add(int a, int b, int c) {


return a + b + c;
}

double add(double a, double b) {


return a + b;
}
}

public class MethodOverloading {


public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println("Sum (int, int): " + calc.add(5, 10));
System.out.println("Sum (int, int, int): " + calc.add(1, 2, 3));
System.out.println("Sum (double, double): " + calc.add(2.5, 3.5));
}
}
Java Programs - Class, Method Overloading, Constructor Overloading

c) Program to implement Constructor Overloading (ConstructorOverloading.java)

class Book {
String title;
int pages;

Book() {
title = "Unknown";
pages = 0;
}

Book(String t) {
title = t;
pages = 100;
}

Book(String t, int p) {
title = t;
pages = p;
}

void display() {
System.out.println("Title: " + title + ", Pages: " + pages);
}
}

public class ConstructorOverloading {


public static void main(String[] args) {
Book b1 = new Book();
Book b2 = new Book("Java Basics");
Book b3 = new Book("OOP Concepts", 250);

b1.display();
b2.display();
b3.display();
}
}

You might also like