PRE-DEFINED CLASSES IN OOP
Object-oriented programming is a major paradigm of programming and we are using it in mostly functional programming now-a-days, although we do not notice it. So, how we are using objects in our daily life. First, we will discuss using objects in real life. After it, we will discuss OOP in functional programming and we will finally go to the original classes built in object oriented programming.
OBJECTS IN REAL LIFE
In our life, we are using term object interchangeably. However, what objects are and how are they related to objects' in python or java. In our life we define object has anything that has some physical existence in world. Similarly, in programming
Objects are implementation of blueprints of classes and they have physical existence on computer memory. An object has its own behavior, identity and state.
Objects are related to classes and in simple scenario when we are talking to objects and classes in real life, we may consider a student class as a class and all the students in that class are objects and each student has its own roll number, name, grade, phone number etc. and these students made a single class. We may consider another real world scenario where we can consider a Employee as a class and all employees of a specific company as objects of that class.
OBJECTS IN PROGRAMMING
As we know the concept of objects in real life, now its time to implement those concepts in programming language. The language we use for implementation of objects is Java, although you can also use language of your own interest like C++ or python also but the core concept remains same. For an object you have to consider three important things to know:
- An object should have its own identity. It should be distinguished from other objects of same class with same states and behaviors.
- An object should have its own state. Instance variables define the state of objects.
- An object must have its own behavior and methods define the behavior of objects in a class for specific instance.
Object have unique identity means that we have to define unique name for object each time. In java, we can create a new object using new keyword that will create object in heap memory and its reference value is stored in reference variable. Let's consider this code for instance of class Employee,
In the above code, we have seen in first line Employee cashier which is likely to create a object but this is wrong. In line1, we have only created a reference variable, cashier, that has the ability to store reference to some object. In the line 2, we tried to call the method of class Employee (for simplicity, we assume class Employee has method getSalary() ) and it will show error because cashier is just an reference variable with no reference to object in it. So, it is showing compilation error.
On the other hand, we are creating a new object on line 4, using new keyword and using class constructor which we will discuss later on. In line 4, new object has created and now this time, reference has stored in cashier and it is now containing the memory address of object on heap not on stack. At this time, when we try to call the method of class, it works fine as in line 5.
PRE-DEFINED CLASSES IN JAVA
In Java, we are seldom suing pre-defined classes in functional programming also, when we are not implementing object oriented programming concepts in our code at all. So, in this situation how we are using objects. Let's consider a pre-built class Math in java which has different static methods.
Why Math class has static methods instead of non-static?Because we generally use static methods for return types of numbers and their is no need to create objects to calculate values and store them in separate objects.
So, let's consider this example when we call some method of Math class using Math.methodname() but this only applies to Math class. When dealing with other classes like Date class, we have to create a object for that class so that we can access methods of that class. In the other hand, when we are trying to call methods of one class into another we are doing this by using objects.
Where we actually use objects in Java using pre-defined class?We usually create a object for Scanner class to take input from keyboard, which is an example of using objects in functional programming also, although we are not directly using objects for own user-defined classes.
There are many other examples where you can find we are implementing java in objects and classes notation. When we have to verify date, we use Date class and create object for it and there are many other classes for calendar and other date formats also which we have to use in general.
Now consider this code, where we are using pre-defined classes and creating objects to use them .
CONCLUSIONClasses are blueprints of objects and each object has its own identity, behavior and state. Objects are created on heap memory and reference variables are used to store their memory address to refer them as shown above in diagram.
