Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Java Class_Object_Constructor

The document contains multiple Java class examples demonstrating object-oriented programming concepts such as class definition, constructors (default, parameterized, copy), methods, and encapsulation with getters and setters. Each example includes a main class that creates objects and calls methods to display or manipulate data. The code snippets illustrate various features of Java, including constructor overloading and private constructors.

Uploaded by

Bhargavi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Class_Object_Constructor

The document contains multiple Java class examples demonstrating object-oriented programming concepts such as class definition, constructors (default, parameterized, copy), methods, and encapsulation with getters and setters. Each example includes a main class that creates objects and calls methods to display or manipulate data. The code snippets illustrate various features of Java, including constructor overloading and private constructors.

Uploaded by

Bhargavi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1. Simple class-object oriented program.

// Define a class named Car


class Car {
// Attributes (fields)
String brand;
int speed;
// Constructor
Car(String brand, int speed) {
this.brand = brand;
this.speed = speed;
}
// Method to display car details
void displayInfo() {
System.out.println("Car Brand: " + brand);
System.out.println("Speed: " + speed + " km/h");
}
}
// Main class
public class Main {
public static void main(String[] args) {
// Creating an object of Car class
Car myCar = new Car("Toyota", 120);
// Calling the method
myCar.displayInfo();
}
}
2. Class with Methods (Simple Calculator)
// Calculator class
class Calculator {
// Method to add two numbers
int add(int a, int b) {
return a + b;
}
// Method to multiply two numbers
int multiply(int a, int b) {
return a * b;
}
}
// Main class
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator(); // Creating an object
// Using methods
int sum = calc.add(5, 3);
int product = calc.multiply(4, 2);
// Display results
System.out.println("Sum: " + sum);
System.out.println("Product: " + product);
}
}
3. Class with Constructor

// Student class
class Student {
// Attributes
String name;
int age;

// Constructor
Student(String n, int a) {
name = n;
age = a;
}

// Method to display student details


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

// Main class
public class Main {
public static void main(String[] args) {
Student s1 = new Student("Alice", 20);
Student s2 = new Student("Bob", 22);

s1.display();
s2.display();
}
}
4. Class with Getter and Setter Methods

// Person class
class Person {
private String name;

// Setter method
void setName(String n) {
name = n;
}

// Getter method
String getName() {
return name;
}
}

// Main class
public class Main {
public static void main(String[] args) {
Person p = new Person();
p.setName("John");
System.out.println("Person's name: " + p.getName());
}
}
5. Java program with class A having a default constructor,
instance variables (int a, String name), and a method
show() that prints the values. Class B contains the main
method.

class A {
// Instance variables
int a;
String name;
// Default constructor
A() {
a = 10; // Default value for 'a'
name = "John"; // Default value for 'name'
}
// Method to display values
void show() {
System.out.println("Value of a: " + a);
System.out.println("Name: " + name);
}
}
// Class B (Main class)
public class B {
public static void main(String[] args) {
// Creating an object of A
A obj = new A();
// Calling show() method
obj.show();
}
}
6. program without a default constructor. Since Java
provides default values for instance variables (int → 0,
String → null), we don’t need to explicitly initialize them.
class A {
// Instance variables (initialized by default)
int a; // Default value: 0
String name; // Default value: null

// Method to display values


void show() {
System.out.println("Value of a: " + a);
System.out.println("Name: " + name);
}
}

// Class B (Main class)


public class B {
public static void main(String[] args) {
// Creating an object of A
A obj = new A();

// Calling show() method


obj.show();
}
}
7. Example of a parameterized constructor in Java.

// Class A with a parameterized constructor


class A {
// Instance variables
int a;
String name;
// Parameterized constructor
A(int x, String y) {
a = x; // Assigning parameter value to instance variable
name = y;
}
// Method to display values
void show() {
System.out.println("Value of a: " + a);
System.out.println("Name: " + name);
}
}
// Class B (Main class)
public class B {
public static void main(String[] args) {
// Creating objects with different values
A obj1 = new A(10, "Alice");
A obj2 = new A(20, "Bob");
// Calling show() method
obj1.show();
obj2.show();
}
}
8. Java program demonstrating a copy constructor.

class A {
int a; String name; // Instance variables
// Parameterized constructor
A(int x, String y) {
a = x; name = y;
}
// Copy constructor
A(A obj) {
a = obj.a; name = obj.name;
}
// Method to display values
void show() {
System.out.println("Value of a: " + a);
System.out.println("Name: " + name);
}
}
public class B {
public static void main(String[] args) {
// Creating an object using the parameterized constructor
A obj1 = new A(10, "Alice");
// Creating a copy of obj1 using the copy constructor
A obj2 = new A(obj1); // Displaying values of both objects
System.out.println("Original Object:");
obj1.show();
System.out.println("\nCopied Object:");
obj2.show();
}
}
9. private constructor

// Class A with a private constructor


class A {
// Instance variables
private int a;
private double b;
private String c;

// Private constructor
private A() {
a = 10;
b = 58.52;
c = "Amit";

// Printing values inside constructor


System.out.println("Inside Private Constructor:");
System.out.println("Value of a: " + a);
System.out.println("Value of b: " + b);
System.out.println("Value of c: " + c);
}

// Main method inside the same class


public static void main(String args[]) {
// Creating an object directly inside the class
A ref = new A();
}
}
10. Java Program: Constructor Overloading

// Class A with overloaded constructors


class A {
// Instance variables (public for direct access)
int a;
double b;
String c;

// Default Constructor
A() {
a = 100;
b = 53.55;
c = "Amit";
}

// Constructor with one int parameter


A(int x) {
a = x;
}

// Constructor with double and String parameters


A(double y, String z) {
b = y;
c = z;
}
}
// Class B to create objects and print values
public class B {
public static void main(String[] args) {
// Creating objects with different constructors
A r1 = new A(); // Calls default constructor
A r2 = new A(10); // Calls constructor with int
A r3 = new A(25.5, "Hello"); // Calls constructor with double and
String

// Printing values in class B


System.out.println("Object r1: a = " + r1.a + ", b = " + r1.b + ", c =
" + r1.c);
System.out.println("Object r2: a = " + r2.a + ", b = " + r2.b + ", c =
" + r2.c);
System.out.println("Object r3: a = " + r3.a + ", b = " + r3.b + ", c =
" + r3.c);
}
}

You might also like