Objects and classes:
Java is an object-oriented programming language. The core concept of the object-
oriented approach is to break complex problems into smaller objects.
An object is any entity that has a state and behavior. For example, a bicycle is an
object. It has
States: idle, first gear, etc
Behaviors: braking, accelerating, etc.
Before we learn about objects, let's first know about classes in Java.
Java Class: A class is a blueprint for the object. Before we create an object, we
first need to define the class.
We can think of the class as a sketch (prototype) of a house. It contains all the
details about the floors, doors, windows, etc. Based on these descriptions we build
the house. House is the object.
Since many houses can be made from the same description, we can create many
objects from a class.
We can create a class in Java using the class keyword. For example,
class ClassName {
// fields
// methods
}
Here, fields (variables) and methods represent the state and behavior of the object
respectively.
fields are used to store data
, methods are used to perform some operations
For our bicycle object, we can create the class as
class Bicycle {
// state or field
private int gear = 5;
// behavior or method
public void braking() {
System.out.println("Working of Braking");
}
}
In the above example, we have created a class named Bicycle. It contains a field
named gear and a method named braking().
Here, Bicycle is a prototype. Now, we can create any number of bicycles using the
prototype. And, all the bicycles will share the fields and methods of the prototype.
What is an Object in Java?
Objects model an entity that exists in the real world. Modeling entities require you
to identify the state and set of actions that can be performed in that object. This
way of thinking is key to object-oriented programming. It is important to
disambiguate an Object with an instantiated object in Java.
An Object is the root class of all instantiated objects in Java.
Instantiated objects are names that refer to an instance of the class.
Declaring Objects in Java
Sphere sphere = new Sphere(10);
,// Java Program for class example
class Student {
// data member (also instance variable)
int id;
// data member (also instance variable)
String name;
public static void main(String args[])
{
// creating an object of
// Student
Student s1 = new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}
, Difference between Java Class and Objects: The differences between class and
object in Java are as follows:
Class Object
Class is the blueprint of an object. It is
An object is an instance of the class.
used to create objects.
No memory is allocated when a class Memory is allocated as soon as an
is declared. object is created.
An object is a real-world entity such as
A class is a group of similar objects.
a book, car, etc.
Class is a logical entity. An object is a physical entity.
Objects can be created many times as
A class can only be declared once.
per requirement.
Objects of the class car can be BMW,
An example of class can be a car.
Mercedes, Ferrari, etc.
What are Branching Statements in Java?
Branching statements allow the flow of execution to jump to a different part of the
program. The common branching statements used within other control structures
include: break, continue, and return.
Types of Branching Statement in Java:
In Java, there are three Branching Statements. They are as follows: