0% found this document useful (0 votes)
11 views4 pages

Class and Object

The document provides an overview of classes and methods in Java, emphasizing that classes serve as blueprints for creating objects and defining their behaviors through methods. It includes examples of class declaration, object instantiation, and method usage, illustrating how to assign values and return results. Additionally, it explains the concept of object reference variables and how they relate to memory management in Java programming.

Uploaded by

krishnapate2024
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)
11 views4 pages

Class and Object

The document provides an overview of classes and methods in Java, emphasizing that classes serve as blueprints for creating objects and defining their behaviors through methods. It includes examples of class declaration, object instantiation, and method usage, illustrating how to assign values and return results. Additionally, it explains the concept of object reference variables and how they relate to memory management in Java programming.

Uploaded by

krishnapate2024
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/ 4

 Title: Classes and Methods in Java

Java is an object-oriented programming language. Classes and objects are fundamental building
blocks.
 This presentation covers:
o Class fundamentals
o Declaring objects
o Assigning object reference variables
o Adding methods to a class

Class Fundamentals

We are using classes and objects in programming to solve the real world problem through
coding.

 A class is a blueprint for creating objects. (Without Class we cannot create objects)

object like Car, Mobile ,Pen

So for creating any object like Car,Mobile ,Pen etc we have to Specify its Properties like
Model, Color, Size And the Functions like automatic manual… etc. These are specifying
inside the class.

So in programming the Objects are all the entities like Object(Car ,pen…), Person, Animal or
anything.

Different object have different properties like Pen (Color ,size, type(gel pen ball pen..)

class ClassName
{
// Fields (Instance Variables- that hold object data)
DataType variableName;

// Methods that define object behavior like in car getSpeed, startEngine

ReturnType methodName() {
// Method body
}
}
class → Keyword used to define a class.

 ClassName → class name should be start with upper case letter. Ex Student, Parent, MyClass

Example of a Class

 The example below demonstrates a simple Java class named Car with attributes and a method.

Code Example:
class Car {
String model; // Stores the car's model name
int year; // Stores the car's manufacturing year

void displayInfo() { // Methods allow objects to perform actions.

System.out.println("Model: " + model);


System.out.println("Year: " + year);
}
}

Declaring Objects

 Objects are instances of a class.


 Syntax for creating an object:
ClassName objectName = new ClassName();

 Example:
Car myCar = new Car();

How This Class is Used in a Program:


public class Main { // A Main class: Contains the main method that creates an object and calls methods.
public static void main(String[] args) {
Car myCar = new Car(); // Creating an object of the Car class
myCar.model = "Tesla Model S"; // Assigning values to fields
myCar.year = 2023;

myCar.displayInfo(); // Calling the method to print details


}
}
Expected Output:

Model: Tesla Model S


Year: 2023

Assigning Object Reference Variables

 Objects are accessed via reference variables.


 Example:
Car car1 = new Car();
Car car2 = car1; // Both car1 and car2 refer to the same object.
 Changing car2 also affects car1 because they refer to the same object and same memory location,

Creating and Using Objects with Methods


public class Main {
public static void main(String[] args) {
// Step 1: Creating an object of the Car class
Car myCar = new Car();
// Step 2: The setDetails method is called using myCar & Assign values to it.
myCar.setDetails("Tesla Model X", 2023); //  .

 It assigns "Tesla Model X" to the model attribute.


 It assigns 2023 to the year attribute.
// Step 3: Calling a method to display car details
myCar.displayInfo();
}
}
Expected Output:
Model: Tesla Model X
Year: 2023

Summary

 A class is a template for creating objects.


 Objects are instances of a class.
 Methods define behaviors of objects.
 Object reference variables store addresses of objects.

Returning a Value in Java Methods


In Java, the return type of a method specifies the type of value the method will return when it is called. If a
method does not return any value, it should have a return type of void.

returnType → The data type of the value the method will return (e.g., int, double,
String,Boolean….)

Method with a Return Value

Example:-
public class Example {
// Method returning an integer
public int getNumber() {
return 10;
}
// Method returning a double
public double getPrice() {
return 99.99;
}
// Method returning a boolean
public boolean isAvailable() {
return true;
}
}

Example: Method Returning an Integer

class Calculator {
// Method to add two numbers and return the result
int add(int a, int b) {
return a + b; // Returns the sum of a and b
}
}

public class Main {


public static void main(String[] args) {
Calculator calc = new Calculator(); // Creating an object
int result = calc.add(5, 7); // Calling the method and storing the
returned value
System.out.println("Sum: " + result); // Output: Sum: 12
}
}

✅ The method add(int a, int b) returns an int value.


✅ The returned value is stored in the variable result.
✅ System.out.println(result); prints the returned sum. i.e. (Sum: 12)

You might also like