• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Class and Object in Java with Real Time Example and Program

Pranati Paidipati/39192/3
CodeJAVA

Objects and classes are the elementary building blocks of the object-oriented programming system. In this tutorial, we will learn about how classes and objects make the foundational components of the OOP system.

It also covers class and object in Java with real time example along with programming.

What is a class?

A class is considered to be the template which is used to create a group of similar objects. In other words, a class defines what data the object will contain and how it will behave.

The basic syntax used for creating a class is as follows:

class <class_name>
{
	data members;	//hold the data or information
	member functions; //determine the behaviour
}

For example:

class Car {
	String model;						
	String colour;	//data members
	void startCar() {
	    System.out.println(“Car Starts !”);
	}
	void changeGears(){ //member function
    	    System.out.println(“Gears changed !”);
	}
}

What is an object?

An entity with some state and behavior is referred to as an object. Objects can be tangible or intangible.

Object Examples: pen, car, bike, table, chair, mobile, etc.

The characteristics an object defines include state, behavior, and identity. The state of an object refers to the basic data it represents, behavior represents the functionality of the object and identity is the unique identification of the object.

Let’s consider a smartphone as an object. It is manufactured by Motorola, model name is G6, color is indigo black are regarded as the state of the smartphone. It is used to make calls, click wonderful pictures, and so these become the functionalities it provides.

Now, let’s have a look at how classes and objects are different with the help of a detailed example.

Difference between Class and Object in Java:

Consider a car and remodel it into a software entity. A car can have characteristics like color, model, mileage and can perform actions like change gears, start the car, stop the car.

So far, we have identified the following things:

Class Name: Cars
Data Members: color, model, mileage
Member Functions: start, change gears, stop.

Sample Program for Object and Class In Java:

Now, for the varied features, varied car objects can be created. Have a look at how this can be achieved with the help of java classes and objects.

public class Cars {
	String colour;
	String model;
	float mileage;
	public void startCar(){
            System.out.println(“Car starts !”);
	}

	public void changeGears(){
	    System.out.println(“Gears changed !”);
	}

	public void stopCar(){
	    System.out.println(“Car stopped !”);
	}
	public String getInfo() {
            return (“Model:” +model + “ Colour:” + colour + “ Mileage:” +mileage);
        }
	
	public static void main (String args[]){
		Cars Suzuki = new Cars();
		Suzuki.model = “Baleno”;
		Suzuki.colour = “Jet Black”;
		Suzuki.mileage = 27.39;
		System.out.println(Suzuki.getInfo());
		Suzuki.startCar();
        }
}

Output:

Model: Baleno Colour: Jet Black  Mileage: 27.39
Car starts!

That’s is all about class and object in Java with real time example.

To sum it up, classes are logical entities that define the blueprint from which objects are created and objects are physical entities with some particular state and behavior. So try practicing some examples of classes and objects in Java to understand them better.

Any question, before we move to next chapter of our Java tutorial?


« Introduction to OOPs Concept in Java Constructor in Java »

JavaJava Oops Concepts
Pranati Paidipati
I am a graduate in computer science with a creative bent of mind for writing content. With great gusto, I enjoy learning new things. I trail in database management system, and object-oriented programming languages like Java, C/C++.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Josette Eggers
    April 9, 2020 at 8:44 pm

    Thanks for this simple explanation. I’m going through these Java tutorials. I find them very simple and easy to understand.

    • Reply
      Pranati Paidipati
      April 13, 2020 at 2:35 pm

      Thank you, Josette. Glad to know that the Java tutorials on csestack.org are helping you in easily understanding the concepts.

  • Reply
    Mandeep Patodi
    December 11, 2022 at 7:50 pm

    I’m a 2nd-year college student. I like the way you explained it in the easy manner.

Leave a Reply Cancel reply

Basic Java Tutorial

  1. Java- Tutorial Overview
  2. Java- Features & Characteristics
  3. Java-  Installation & Setup
  4. Java- Hello, World Program!
  5. Java- JDK vs JVM vs JRE
  6. Java- Data Types & Variables
  7. Java- String & its Methods
  8. Java- Different Operators Types
  9. Java- Flow Control Statements
  10. Java- Array
  11. Java- Exception Handling
  12. Java- ‘throw’ vs ‘throws’ Keyword
  13. Java- RegEx
  14. Java 12- New Advanced Features

Java OOPs concepts

  1. Java- OOPs Introduction
  2. Java- Classes & Objects
  3. Java- Constructor & Overloading
  4. Java- Method Overload vs Override
  5. Java- Access Modifiers
  6. Java- Abstraction
  7. Java- Inheritance
  8. Java- Interfaces
  9. Java- Nested Inner Classes

Java Advanced

  1. Java- Applet vs Application
  2. Java- HashMap Collections
  3. Java- ArrayList
  4. Java- LinkedList
  5. Java- HashSet
  6. Java- HashMap vs HashSet
  7. Java- Reverse Linked List

Java Exercise

50+ Java Coding Questions [Practice]

Java Projects

Patient Billing Software

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard