[go: up one dir, main page]

0% found this document useful (0 votes)
6 views1 page

Classes

Uploaded by

kodurusailalitya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Classes

Uploaded by

kodurusailalitya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

public class MyClass {

// Fields
private int age;
private String name;

// Constructor
public MyClass(int age, String name) {
this.age = age;
this.name = name;
}

// Method
public void displayInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}

// Main method (for demonstration)


public static void main(String[] args) {
// Creating an object of MyClass
MyClass myObject = new MyClass(25, "John");

// Calling a method on the object


myObject.displayInfo();
}
}

Second :

public class Car {


// Attributes
private String brand;
private String model;
private int year;

// Constructor
public Car(String brand, String model, int year) {
this.brand = brand;
this.model = model;
this.year = year;
}

// Method to display car details


public void displayDetails() {
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}

public static void main(String[] args) {


// Creating an object of Car class
Car myCar = new Car("Toyota", "Camry", 2022);

// Calling method to display car details


myCar.displayDetails();
}
}

You might also like