
- Computer Programming - Home
- Computer Programming - Overview
- Computer Programming - Basics
- Programming - Environment
- Programming - Basic Syntax
- Programming - Data Types
- Computer Programming - Variables
- Computer Programming - Keywords
- Computer Programming - Operators
- Computer Programming - Decisions
- Computer Programming - Loops
- Computer Programming - Numbers
- Programming - Characters
- Computer Programming - Arrays
- Computer Programming - Strings
- Computer Programming - Functions
- Computer Programming - File I/O
- Computer Programming - OOP Concepts
- Computer Programming - Class and Object
- Computer Programming - Dynamic Memory Management
- Algorithms & Their Complexity
- Computer Programming - Summary
Computer Programming - Class and Object
What is Class?
Classes and objects are key concepts in object-oriented programming (OOP), allowing developers to develop programs based on real-world scenarios. A class functions as a blueprint or template for creating objects. It specifies a collection of data members and member functions or methods.
Overall, a class is a user-defined data type. It is a blueprint that defines the structure and behavior of its objects, ensuring abstraction and modularity. It encapsulates data members (data members i.e. variables or attributes) and member functions (methods or member functions) in a single unit.
Structure of a Class
The structure of an Object-Oriented Program is organized into specific sections that define its flow and functionality. A typical OOPs program begins with pre-processor directives, such as #include, which includes libraries. This is followed by the class declarations using class keyword.
Header files inclusion class class_name { Private: ... ... data members ... ... Public: ... ... member functions ... ... }
A class is having two sections i.e. data member and member function. Data member section initiates with private access specifier which includes class attributes used in functions to hold data values.
Public section of the class includes member functions of the class. These are the functions which performs actions during execution of the class. All the functions which are defined in public section of the class can be accessible outside of the class.
The execution of a program begins through main() function. It often comprises the program's basic logic, which is organized into statements, control structures, and function calls. Additional user-defined functions can be written outside of the main() function in order to help modularize and code reusability. A program may optionally include class and object declarations for object-oriented programming, as well as namespaces for logical code organization. This structured technique improves software efficiency, modularity, and maintainability.
How to Declare a Class?
A class can be declare using class keyword followed by class name. A keyword is a reserve word of a program which has its predefine meaning that cannot be changed. A class name is user defined.
Syntax
class demo { private: .. // Data members section .. public: { .. // Member functions section .. } };
How to Create an Object?
An object is an instance of a class. It is created in main function followed by class name.
Syntax
class demo { private: ... // Data members section ... public: { ... // Member functions section ... } }; int main() { demo d; // d is an object of demo class ... ... }
Class and object in C++
In OOPs, a class is a user defined data type that defines data members and member functions or methods. It encapsulates data member and member functions together. It provides a structured way to model real-world entities in software.
Read More: C++ Classes and Objects
Example
#include <iostream> using namespace std; class Sample { public: string msg; // data member/attribute / variable void display() // Method { cout << "Print Message: " << msg << endl; } }; int main() { Sample s; // Create an object s of Sample class s.msg = "This is a simple message using class"; // Set attribute s.display(); // Call method return 0; }
Explanation
- Class Declaration − A class is defines with class keyword; for example class Sample.
- Public Members − string msg is a public data member, and display() is a public member function.
- Object Creation − In main() function, an object is created and its members are accessed directly. For example
Sample s; // Create an object s of Sample class s.msg = "This is a simple message using class"; // Set attribute s.display(); // Call method return 0;
Class and object in Java
Class and object are the foundation of object-oriented programming in Java. In Java, a class is a user defined data type that defines the structure and behavior of objects by specifying data members and member functions or methods. It functions as a template for constructing instances of objects. An object is a class instance that accesses class's data members and member functions and can conduct actions through its methods.
How to Declare a Class in Java?
A class can be declare using class keyword followed by class name. A keyword is a reserve word of a program which has its predefine meaning that cannot be changed. A class name is user defined.
For example
class student { // Data members of the class String name; . // Method . }
How to Create an Object
An object is an instance of the class. In Java, object is created in main class. Following example demonstrates that how to create an object in Java.
// Main class to create objects public class Main { public static void main(String[] args) { // Create an object of the defined class . // Access the object's methods } }
Example to create a class in Java
import java.util.*; public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Explanation
import java.util in Java is a statement that allows access to the java.util package, which contains utility classes like ArrayList, HashMap, Scanner, Date, and others. public static void main(String[] args) method is defined in the public class Main. The public access modifier ensures that the class can be accessed by anybody in the project, and Main is typically the class name that starts program execution.
In Java, System.out.println is a predefine function that is used to print a message or output to the console, followed by a newline. It is part of the System class, where out is a static PrintStream object, and println is its function or method for printing output.
How to Create a Class and Object in Java?
In Java, a class can be created using class keyword followed by class name. Following example demonstrates to define a class and create its object.
Example
// Define a class class student { // Data members of the class String name; int rollno; // Constructor to initialize the attributes student(String name, int rollno) { this.name = name; this.rollno = rollno; } // Method to display student details void display() { System.out.println("Name: " + name); System.out.println("Roll Number: " + rollno); } } // Main class to create objects public class Main { public static void main(String[] args) { // Create an object of the student class student s = new student ("Tutorialspoint", 1001); // Access the object's methods s.display(); } }
Class and object in Python
In Python, a class is a user defined data type in which a user defines its structure and functionalities that need to be perform during execution of this class. It is defined using the class keyword, followed by the class name. It can be considered as a blueprint for creating objects, encapsulating data members and member functions or methods in a single unit.
For example, a class student might include attributes like name and age, and methods like details() to describe characteristics of a student. The class itself contains no data; rather, it offers a foundation for generating customized instances.
An object is an instance of a class. To create an object in Pyhton, we use __init__ method (a constructor) to initialize its attributes with specific values. For example, you can create an object student1 from the student class and assign it attributes like name="Krishna K." and age=25. Each object created by the same class can have distinct data in spite of using the same methods. Objects support data encapsulation, which means that their internal state may only be accessed and modified through the methods they describe, ensuring modularity and consistency.
How to Declare a Class in Python?
In Python, a class is declared using the class keyword followed by the class name and a colon. The class contains data members and member functions that define its behavior and properties.
Syntax
class ClassName: .... # class body .... # Programming statements
How to Create an Object?
An object is an instance of the class. After successfully designed a class, its functionalities can be accessible by creating its object.
Object_name = class_name # Create an object of the class ...... # Programming statements ...... # Programming statements
How to Create a Class and Object in Python?
In Python, a class is declared using the class keyword followed by the class name and a colon. Object can be created using Object_name = class_name. Classes and objects work together to build software systems that are resilient, scalable, and easy to maintain.
Example
class student: # Constructor to initialize attributes def __init__(self, name, age): self.name = name # Instance attribute self.age = age # Instance attribute def describe(self): # Method to details about student return f"Students common deatils are {self.name} {self.age}." s1 = student("Anirudhha", "25") # Create an object of student class print(s1.describe()) # Output: Students common deatils are Anirudhha 25.
Explanation
-
Class Definition (class student) − The student class is the blueprint. It has
- Attributes − name and age initialized via the __init__ method.
- Methods − describe, which has personal details of the student.
-
Object Creation (s1):
- s1 is an instance of the student class; it is an object.
-
Method Calls:
- The describe method showing students personal details like its name and age.
Classes and objects implement encapsulation and abstraction in OOP. Encapsulation ensures that an object's underlying state is hidden and only available using specified interfaces, hence increasing security and lowering complexity. Abstraction enables developers to focus on critical features while masking implementation details, making software systems easier to maintain and improve.
What is an Object?
An object is an instance of a class; object inherits all the data member and member functions defined in the class. Objects encapsulate data and functionality, allowing for modularity and code reuse. For example, if you have a class Student, objects like s1 and s2 represent specific students with their own skills, such as subject expertise, sports achievements, star performers etc. The object concept of OOP made C++ an effective object-oriented language by enabling data abstraction and modular programming features.
int main() { class_name object_name; ... ... ... }
Important facts about a class in Object Oriented Programming
Some of the important facts about classes and objects in OOPs are as follows −
- Object − An object is an instance of a class. A class is actively performs its functioning after creating its object.
- Subclass − A class can have subclasses i.e. parent class and child classes. A child class inherits some or all of the properties of its base class or parent class.
- Class hierarchy − The structure of a class and its subclasses is called the class hierarchy.
- Data Members − Data members represent the state of an object, and are the characteristics that distinguish classes.
- Member Functions or Class methods − Methods are functions which are specifically created to perform specific tasks; class methods use data members to store values and perform computations, Member functions are defined inside a class.
Classes are a fundamental concept of OOPs that makes program development more efficient and organized. A class allows code reusability, modularity, and code organization, making complicated systems easier to manage.