OOPS Concepts: Classes and Objects


In this article, I am going to explain about what is OOPS concept, What is class, example of class, what is object, example of object, what is constructor, constructor overloading, Default constructor and parametrized constructor, Difference between Default constructor and parametrized constructor, What is destructor, Memory allocation of Java program in detail.

What is OOPS?

OOPS stands for Object Oriented Programming System.

Object oriented means we organize our software as a combination of different types of objects that incorporates both state and behavior.
It simplifies software development and maintenance by providing some rules.
Basically OOPS is a design philosophy. It is standard or methodology for designing program using Objects and classes.

Basically it consists of below things
  1. Class
  2. Object
  3. Encapsulation
  4. Inheritance
  5. Polymorphism
  6. Abstraction

What is class?

Class is template from which objects are created. State and behavior are written in the class. State means variables and behavior means method. So class contains variables and methods which can be accessed by object.
Basically class is a collection of data members i.e. objects, constructor, methods, interface etc.
class keyword is used to declare a class in java

Example:
    Public class Car{
    String brandName;
    Int number;
        Void drive(){
    System.out.println("Brand Name is:" + brandName);
    System.out.println("Car number is:" + number);
        }
    }

Here, brandName and number are two variables of car class and drive is a method.
So to use this, we should create an object of this class. After object creation, some memory gets allocated to store actual data of variables.

What is object?

  • It is an instance of class.
  • Object is a real world entity. Entity means existing thing which have data and behavior.
  • It can be physical and logical.
  • Example, Physical: Pen, Dog, Chair
    Logical: Employee, Online Banking System
  • Any entity i.e. software package which has state and behavior is called as object.
  • State:
    It is what software object knows.
    Fields/variables store the object's state.
  • Behavior:
    Method/function is used to expose the object's behavior.
    It is what software object can do.
  • For example: In above example, we have created Car class and declared two variables brandName and number which is state of object. Also we have defined one method drive() which is behavior of object.
  • Syntax:
    ClassName objectName = new className();
  • So let's create object for Car in another class which will be main class.
        public class MainCar {
            public static void main(String[] args) {
                Car twoWheeler = new Car();
                twoWheeler.brandName = "Royal Enfield";
                twoWheeler.number = 4561;
                twoWheeler.drive();
            }
        }
  • Output:
    Brand Name is:Royal Enfield
    Car number is:4561
  • Here, we have created object twoWheeler of class 'Car' and used all the variables and methods of 'Car' class to store the data of variables.
  • When object gets created, some memory gets allocate on 'heap'. After object creation, JVM produces a unique identification number called as hash code number.
    We can find hash code number using 'hashCode()' method of object class.
    Every class in Java is inherited from Object class.
New keyword
  • It is java operator that creates the objects.
  • New operator is followed by constructor.
  • All class have default constructor. So for our class car, Car() is constructor.

Few point about constructor

What is constructor?
  • It is used to create object of class.
  • It is invoked at the time of object creation.
  • It has no return type.
  • Name of constructor is same as of name of class.
  • While creating java class default constructor get created automatically.
  • A Constructor is called at the same time when object gets created. JVM first allocate memory to the object and then execute the constructor to initialize the instance variables.
  • Two types of constructor: Default and parameterized
    1. Default – parameters are not provided at the time of creation.
    2. Parameterized – Constructor which have parameters are called as parameterized constructor.

Constructor overloading

Class can have any number of constructors that differ in parameter lists
Suppose in above example which is having two class 'Car' and 'MainCar', we will initialize variables by using two constructors.
    public class Car {
        private String brandName;
        private int number;
        private int numberOfSeats;

    void drive(){
        System.out.println("Brand Name is:" + brandName);
        System.out.println("Car number is:" + number);
        System.out.println("Number of Seats:" + numberOfSeats);
        }

    Car(String s, int i){
        brandName = s;
        number = i;
    }
    Car(String s, int i, int n){
        brandName = s;
        number = i;
        numberOfSeats = n;
    }
}

So, in Car class we have declared three variables brandName, number and numberOfSeats. Also we have defined drive method and two parameterized constructors with different parameter lists. Now we will provide value to constructors while creating objects in Main class.
    public class MainCar {
        public static void main(String[] args) {
            Car fourWheeler1 = new Car("BMW", 34567);
            fourWheeler1.drive();

            Car fourWheeler2 = new Car("City", 67890, 6);
            fourWheeler2.drive();
        }
    }

Output:
Brand Name is:BMW
Car number is:34567
Number of Seats:0

Brand Name is:City
Car number is:67890
Number of Seats:6

Here, for fourWheeler1 object, we have used first constructor which will be providing only two parameters i.e. brandName and number. For fourWheeler2 object, we have used second constructor which will be providing three parameters i.e. brandName, number and numberOfSeats.

Difference between Default constructor and parameterized constructor


Default constructorParameterized constructor
Don't have any parameters.It will have one or more parameters.
Default constructor is called when data is not passed at the time of creating an object.Parameterized constructor is called when data is passed at the time of creation of object.
Example:
Car()
{
brandName = "BMW";
Number = 4567;
}
Example:
Car(String s, int i)
{
brandName = s;
number = I;
}

What is destructor?

It is a special method used to free memory and deallocate resources when use of object is over.

Advantages of oops over procedure oriented programming language
  • OOPS provides more security by hiding data while in procedure oriented language global data is accessible from anywhere.
  • Development and maintenance is easy in OOPS whereas in Procedure oriented language it becomes complicated when project size grows.

Memory allocation of java program


Suppose we have two classes Car and MainCar.
    Public class Car{
    String brandName;
    Int number;
        Void drive(){
    System.out.println("Brand Name is:" + brandName);
    System.out.println("Car number is:" + number);
    }
    }

    public class MainCar {
        public static void main(String[] args) {
        int i;
        int j=10;
        Car twoWheeler = new Car();
        }
    }


When program starts executing, JVM first look for main method and starts execution from there.
Now in our program we have two variables declared i and j. so, memory gets allocated on stack for int i first with '0' value. Because int is primitive data type which directly stores value. Then after that memory gets allocated for j with 10 value.

After this we are creating object of Car class which is twoWheeler. As object is non-primitive data type it stores memory location of object in stack and that memory location points to heap location where all the memory for each parameter in object is allocated.

In our example, we have two variables and one method in Car class. So after creating object of Car class memory gets allocated for two variables and method on heap. And memory location of heap will get store in stack.
Allocation of memory


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: