• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Classes » Objects and Classes in Java
Next →
← Prev

Objects and Classes in Java

By Dinesh Thakur

Objects and classes are the building blocks of OOP. To understand OOP, first we have to know what objects and classes are.

An object is a programming entity in OOP. It has data components representing the present state of the object as well as functions (also called methods). Data components are termed attributes in the context of OOP languages. An area of memory is kept aside to store values for the attributes when an object is created. Functions describe the behaviour of the object and allow the other programming entities to interact with the object.

An object contains attributes (variables) and methods (member functions). Attributes are the data associated with the object and the methods are the functions and code which operate on the data.

A class is a model to define objects. In other words, an object is said to be an instance of a class. An object may be real or abstract. That is, objects can model concrete things such as people, and data entry forms as well as abstractions such as numbers, equations or geometrical concepts.

Consider an example of a class—car. The class car describes all car objects by specifying the data components and the methods that each can have. Various objects defined in the class car could be Maruti 800, Zen, Santro, Accent and so on. The class car has various attributes such as the seating capacity, maximum speed, engine capacity, parking light, current speed and current position. Typical functions of the car class are to find acceleration and set parking lights on. See Figures (a) and (b) for an illustration.

Real-world objects have two characteristics: state and behaviour. For example, consider an object ball. A ball has particular characteristics such as its diameter, colour and elasticity. Formally, we say that these characteristics contribute to the ball’s state of being. We also describe a ball by what it does, such as the fact that it can be thrown, bounced or rolled. These activities define the ball’s behaviour, similarly the object car has state (colour, wheels, current gear, number of gears and so on) and behaviour (braking, changing gears, accelerating, decelerating and so on).Objects and Classes

Software objects are modelled after real-world objects in that they too have state and behaviour. Attributes define the state of the object and methods define the behaviour of the object. That is, a software object maintains its state in one or more attributes. Attributes can have different values at different instances of time; however, values of a particular attribute must have the same data type.

An attribute/variable is an item of data named by an identifier, and a software object implements its behaviour with methods. A method is a function/sub-routine associated with an object.

The state of an object and its behaviour work together. For example, how high a ball bounces depends on its elasticity. An object’s behaviour often modifies its state. For example, when a ball is rolled, its position changes.

An object must also have a unique identity that distinguishes it from all other objects. Two objects can have the same data types of attributes with the same names, and those attributes can have the same values. Therefore two objects could have the same state. There must be a way of distinguishing the two objects and that is done by using an object’s name. In a program, to define an object we need a class.

A class is a model or pattern from which an object is created. In other words, a class is a special module that defines an object.

A class defines the data types of attribute that will be held in an object, and defines code for the methods. After a class was defined, an object can be created from it. The process of creating an object from a class is called instantiation. Every object is an instance of a particular class.

Defining a class is having a name, a set of attributes and a set of methods defined. The attributes will not have any values associated with them when a class is defined; however, when an object is created from this class, the attributes of this object will have values. The values of the attributes .can be accessed and/or modified by the methods of that class and cannot be accessed or modified by the objects of the other class.

The definition of a class must not be confused with that of an object. A class is the outline for an object. But a class is not an object any more than a blueprint. We introduce the notion of data types, which makes easy to understand the difference between objects and classes. Consider a data type integer. We can declare variables to be of type integer. For example, in C, we can do this with the statement


int i, j;

This statement defines two variables i and j of type integer. When this statement is executed, the two variables are created. Similarly, an object can be defined by a class. That is, a class is similar to a data type (data structure) and an object is like a variable created from some class. In other words, a class defines a type and an object is a particular instance of the class. Suppose Circle is a class, the objects A and B can be created from that class, as in C++, where A and B may be circles of different diameters.


Circle A, B;

An object can be viewed from two perspectives. The first is useful during design and implementation of the object. We have to define the attributes that will be held in the object and write the program code for the methods that make the object useful. The second comes in when making use of the services of an object that was already created. When designing a solution to a large problem, we should know only the services that an object provides rather than the details of how the services are accomplished. That is, from the second point of view, an object is a black box. It is a part of a system whose inner workings and structure are currently hidden. The rest of the system only interacts with the object through a well-defined set of services (called messages) that it provides.

In OOP, objects communicate with one another by sending and receiving information using messages. For an object, a message is a request for execution of a method. When a request is made to an object, it invokes the specified method in the receiving object, then the receiving object responds by generating the desired result. Message passing involves specifying the name of the object, the name of the method and any information that needs to be sent.

You’ll also like:

  1. Java Classes and Objects (With Example) – Definition
  2. What is Classes Objects
  3. Adding the Contents of Two objects by passing objects as parameter.
  4. Returning Objects from Methods in Java
  5. How to Creating Objects and Allocate Memory for Objects in c++
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW