Class and Objects in JAVA 1
What is? Java is an object-oriented programming (OOP) 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. 2
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. 3
Create a class in Java We can create a class in Java using the class keyword. For example, 4 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
Create a class in Java For our bicycle object, we can create the class as 5 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.
Object? An object is called an instance of a class. For example, suppose Bicycle is a class then; • MountainBicycle. • SportsBicycle. • TouringBicycle, etc. can be considered as objects of the class. 6
Creating an Object in Java Here is how we can create an object of a class. 7 We have used the new keyword along with the constructor of the class to create an object. Constructors are similar to methods and have the same name as the class. For example, Bicycle() is the constructor of the Bicycle class. Here, sportsBicycle and touringBicycle are the names of objects. We can use them to access fields and methods of the class. As you can see, we have created two objects of the class. We can create multiple objects of a single class in Java.
Access Members of a Class We can use the name of objects along with the . operator to access members of a class. For example, 8
Access Members of a Class In the above example, we have created a class named Bicycle. It includes a field named gear and a method named braking(). Notice the statement, 9 Here, we have created an object of Bicycle named sportsBicycle. We then use the object to access the field and method of the class. sportsBicycle.gear - access the field gear sportsBicycle.braking() - access the method braking()
Example: Java Class and Objects 10 Output:
Example: Create objects inside the same class 11 Output: Note that in the previous example, we have created objects inside another class and accessed the members from that class. However, we can also create objects inside the same class. Here, we are creating the object inside the main() method of the same class.

Class and Objects in object-oriented programming with Java

  • 1.
  • 2.
    What is? Java isan object-oriented programming (OOP) 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. 2
  • 3.
    Class? • A classis 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. 3
  • 4.
    Create a classin Java We can create a class in Java using the class keyword. For example, 4 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
  • 5.
    Create a classin Java For our bicycle object, we can create the class as 5 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.
  • 6.
    Object? An object iscalled an instance of a class. For example, suppose Bicycle is a class then; • MountainBicycle. • SportsBicycle. • TouringBicycle, etc. can be considered as objects of the class. 6
  • 7.
    Creating an Objectin Java Here is how we can create an object of a class. 7 We have used the new keyword along with the constructor of the class to create an object. Constructors are similar to methods and have the same name as the class. For example, Bicycle() is the constructor of the Bicycle class. Here, sportsBicycle and touringBicycle are the names of objects. We can use them to access fields and methods of the class. As you can see, we have created two objects of the class. We can create multiple objects of a single class in Java.
  • 8.
    Access Members ofa Class We can use the name of objects along with the . operator to access members of a class. For example, 8
  • 9.
    Access Members ofa Class In the above example, we have created a class named Bicycle. It includes a field named gear and a method named braking(). Notice the statement, 9 Here, we have created an object of Bicycle named sportsBicycle. We then use the object to access the field and method of the class. sportsBicycle.gear - access the field gear sportsBicycle.braking() - access the method braking()
  • 10.
    Example: Java Classand Objects 10 Output:
  • 11.
    Example: Create objectsinside the same class 11 Output: Note that in the previous example, we have created objects inside another class and accessed the members from that class. However, we can also create objects inside the same class. Here, we are creating the object inside the main() method of the same class.