0% found this document useful (0 votes)
10 views

Classes and Objects in JAVA

The document provides an overview of Java classes and objects, explaining that a class serves as a blueprint for creating objects, which are instances of that class. It details the properties of classes, types of class variables, and the syntax for declaring classes and creating objects in Java. An example class 'Dog' illustrates how to define attributes and methods, as well as how to instantiate and use an object of that class.

Uploaded by

k58462549
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)
10 views

Classes and Objects in JAVA

The document provides an overview of Java classes and objects, explaining that a class serves as a blueprint for creating objects, which are instances of that class. It details the properties of classes, types of class variables, and the syntax for declaring classes and creating objects in Java. An example class 'Dog' illustrates how to define attributes and methods, as well as how to instantiate and use an object of that class.

Uploaded by

k58462549
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/ 6

OBJECT ORIENTED PROGRAMMING

UNIT-2
What are Java Classes?
A class is a blueprint from which individual objects are created (or, we can say a
class is a data type of an object type). In Java, everything is related to classes and
objects. Each class has its methods and attributes that can be accessed and
manipulated through the objects.
For example, if you want to create a class for students. In that case, "Student" will
be a class, and student records (like student1, student2, etc) will be objects.
We can also consider that class is a factory (user-defined blueprint) to produce
objects.

Properties of Java Classes


 A class does not take any byte of memory.
 A class is just like a real-world entity, but it is not a real-world entity. It's a
blueprint where we specify the functionalities.
 A class contains mainly two things: Methods and Data Members.
 A class can also be a nested class.
 Classes follow all of the rules of OOPs such as inheritance, encapsulation,
abstraction, etc.
Types of Class Variables
A class can contain any of the following variable types.
Local variables − Variables defined inside methods, constructors or blocks are
called local variables. The variable will be declared and initialized within the
method and the variable will be destroyed when the method has completed.

Instance variables − Instance variables are variables within a class but outside
any method. These variables are initialized when the class is instantiated. Instance
variables can be accessed from inside any method, constructor or blocks of that
particular class.
Class variables − Class variables are variables declared within a class, outside
any method, with the static keyword.
Creating (Declaring) a Java Class
To create (declare) a class, you need to use access modifiers followed
by class keyword and class_name.
Syntax to create a Java class
Use the below syntax to create (declare) class in Java:
access modifier class class_name {
data members;
constructors;
methods;
...;
}
Example of a Java Class
In this example, we are creating a class "Dog". Where, the class attributes
are breed, age, and color. The class methods are setBreed(), setAge(), setColor(),
and printDetails().
// Creating a Java class
class Dog {
// Declaring and initializing the attributes
String breed;
int age;
String color;

// methods to set breed, age, and color of the dog


public void setBreed(String breed) {
this.breed = breed;
}
public void setAge(int age) {
this.age = age;
}
public void setColor(String color) {
this.color = color;
}

// method to print all three values


public void printDetails() {
System.out.println("Dog detials:");
System.out.println(this.breed);
System.out.println(this.age);
System.out.println(this.color);
}
}
What are Java Objects?
An object is a variable of the type class, it is a basic component of an object-
oriented programming system. A class has the methods and data members
(attributes), these methods and data members are accessed through an object.
Thus, an object is an instance of a class.
If we consider the real world, we can find many objects around us, cars, dogs,
humans, etc. All these objects have a state and a behavior.
If we consider a dog, then its state is - name, breed, and color, and the behavior
is - barking, wagging the tail, and running.
If you compare the software object with a real-world object, they have very
similar characteristics. Software objects also have a state and a behavior. A
software object's state is stored in fields and behavior is shown via methods. So,
in software development, methods operate on the internal state of an object, and
the object-to-object communication is done via methods.
Creating (Declaring) a Java Object
As mentioned previously, a class provides the blueprints for objects. So basically,
an object is created from a class. In Java, the new keyword is used to create new
objects.
There are three steps when creating an object from a class −
 Declaration − A variable declaration with a variable name with an object
type.
 Instantiation − The 'new' keyword is used to create the object.
 Initialization − The 'new' keyword is followed by a call to a constructor.
This call initializes the new object.
Syntax to Create a Java Object
Consider the below syntax to create an object of the class in Java:
Class_name object_name = new Class_name([parameters]);

Note: parameters are optional and can be used while you're using constructors in the class.

Example to Create a Java Object


In this example, we are creating an object named obj of Dog class and accessing
its methods.
// Creating a Java class
class Dog {
// Declaring and initializing the attributes
String breed;
int age;
String color;

// methods to set breed, age, and color of the dog


public void setBreed(String breed) {
this.breed = breed;
}
public void setAge(int age) {
this.age = age;
}
public void setColor(String color) {
this.color = color;
}

// method to print all three values


public void printDetails() {
System.out.println("Dog detials:");
System.out.println(this.breed);
System.out.println(this.age);
System.out.println(this.color);
}
}

public class Main {


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

// setting the attributes


obj.setBreed("Golden Retriever");
obj.setAge(2);
obj.setColor("Golden");
// Printing values
obj.printDetails();
}
}
Output
Dog detials:
Golden Retriever
2
Golden

You might also like