OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 2 : Topic - 1 : Classes Introduction

Unit - 2

Topic 1 : Introduction to Classes

In Java, a class is a user-defined data type that acts as a blueprint or template for creating objects. Once a class is defined, we can create multiple objects from it, each having its own data but sharing the same structure and behavior defined in the class.

Key Points:

  • A class defines what an object will contain (variables) and what it can do (methods).

  • An object is an instance of a class.

  • Instance variables store data for each object.

  • Methods define the operations that can be performed on the data.


General Form of a Java Class

class ClassName { // Instance variables type variable1; type variable2; ... type variableN; // Methods returnType methodName1(parameter-list) { // method body } returnType methodName2(parameter-list) { // method body } ... returnType methodNameN(parameter-list) { // method body } }

Components of a Class:

  • Instance Variables: Data members of the class that hold values for each object.

  • Methods: Functions defined inside the class that operate on instance variables.

  • Members: Both instance variables and methods together are called members of a class.


Example: A Simple Class

class Box { double width; // instance variable double height; // instance variable double depth; // instance variable // Method to calculate and display volume void volume() { System.out.print("Volume is "); System.out.println(width * height * depth); } }

In this example:

  • The class Box has three instance variables: width, height, and depth (all of type double).

  • It has one method, volume(), that calculates and prints the volume of the box using the instance variables.


Creating and Using Objects

Once we have defined a class, we can create objects from it:

class BoxDemo { public static void main(String[] args) { Box myBox = new Box(); // create object // assign values to instance variables myBox.width = 10; myBox.height = 20; myBox.depth = 15; // call the method myBox.volume(); } }

Explanation:

  1. Box myBox = new Box(); → Creates a new object of type Box.

  2. myBox.width = 10; → Assigns values to the object's instance variables.

  3. myBox.volume(); → Calls the volume() method to calculate and display the volume.


Key Notes about Classes in Java

  • A class can have multiple objects, each with independent values for its instance variables.

  • Methods in a class can access and modify the instance variables.

  • Classes help in achieving Object-Oriented Programming (OOP) concepts like encapsulation, inheritance, and polymorphism.


Example Program

Example Program on Java Classes

// Example of a simple Java class and its usage // Class definition class Box { double width; // instance variable double height; // instance variable double depth; // instance variable // Method to calculate and display volume void volume() { double vol = width * height * depth; System.out.println("Volume is " + vol); } } // Main class to test the Box class public class BoxDemo { public static void main(String[] args) { // Create first object of Box Box box1 = new Box(); box1.width = 10; box1.height = 20; box1.depth = 15; // Create second object of Box Box box2 = new Box(); box2.width = 3; box2.height = 6; box2.depth = 9; // Display volumes of both boxes box1.volume(); box2.volume(); } }

Explanation:

  1. Class Definition:

    • The Box class has three instance variables: width, height, and depth.

    • It contains a method volume() to calculate and print the volume.

  2. Creating Objects:

    • Box box1 = new Box(); creates an object box1 of type Box.

    • Box box2 = new Box(); creates another object box2.

  3. Assigning Values:

    • Each object has its own set of instance variables.

    • We assign values to them separately.

  4. Calling Methods:

    • box1.volume(); calls the method for the first object.

    • box2.volume(); calls the method for the second object.


Sample Output:

Example Program on Java Classes

// Example of a simple Java class and its usage // Class definition class Box { double width; // instance variable double height; // instance variable double depth; // instance variable // Method to calculate and display volume void volume() { double vol = width * height * depth; System.out.println("Volume is " + vol); } } // Main class to test the Box class public class BoxDemo { public static void main(String[] args) { // Create first object of Box Box box1 = new Box(); box1.width = 10; box1.height = 20; box1.depth = 15; // Create second object of Box Box box2 = new Box(); box2.width = 3; box2.height = 6; box2.depth = 9; // Display volumes of both boxes box1.volume(); box2.volume(); } }

Explanation:

  1. Class Definition:

    • The Box class has three instance variables: width, height, and depth.

    • It contains a method volume() to calculate and print the volume.

  2. Creating Objects:

    • Box box1 = new Box(); creates an object box1 of type Box.

    • Box box2 = new Box(); creates another object box2.

  3. Assigning Values:

    • Each object has its own set of instance variables.

    • We assign values to them separately.

  4. Calling Methods:

    • box1.volume(); calls the method for the first object.

    • box2.volume(); calls the method for the second object.


Sample Output:

Volume is 3000.0 Volume is 162.0

Comments

Popular posts from this blog

Career Guide - B.Tech Students

Artificial Intelligence - UNIT - 1 Topic - 1 : Introduction to AI (Artificial Intelligence)

Financial Aid for Students: Scholarships from Government, NGOs & Companies