Object Oriented Concepts in Java

Object Oriented Concepts in Java

OOPs allows the creation of classes with properties and methods, that can replicate real-world object characteristics. A structured template with desired properties can be created through classes & simultaneously multiple objects can be created that inherit these properties.

Classes are a named groups of properties & methods that allow the creation of a customized datatype that is a combination of multiple primitive datatypes. Classes allow us to create blueprints that resemble the real world, and power to decide what properties they must have! The class template is by default picked up by every object. Class names begin with a capital letter.

Class is a template for an object -> logical construct
Syntax to create a class

class className {
data variable declarations
}

class Student {
            int rollNo;
            String name;
            float marks;
}

The object is an instance of a class -> physical reality that occupies space in memory

PROPERTIES OF AN OBJECT

  • State of an object -> value from its datatype

  • Identity of an object -> where is the value stored [stack/heap]

  • Behaviour of an object -> effect of the datatype operations

    Declaring and initializing Objects :

    className referenceVariable = new className();

      public static void main(String[] args) {
              Student studentInstance = new Student();
      }
    

    declaration of object [just present in stack memory]
    -> className referenceVariable ;

  • Student studentInstance;

initializing of object \[dynamically allocates memory at runtime\] , in Heap & returns a reference \[connecting to stack reference\]  
\-> r**eferenceVariable = new className();**
  • studentInstance = new Student();

    HOW TO ACCESS PROPERTIES OF THE OBJECTS -> DOT OPERATOR

The dot operator(separator) links the reference variable of an object with its instance variables.  
Assigning values to these properties:

```java
        studentInstance.rollNo = 44;
        studentInstance.marks = 88.4f;
        studentInstance.name = "Paige Spara";
```

CONSTRUCTOR :


class referenceVariable = new className();  
className 2nd time indicates constructor. A constructor has the same name as the class name. Constructors are a special type of function that runs automatically when an object is created. When no constructor is written, a default constructor is called \[empty/ no arguments constructor is called\]  

**default constructor :**
Student newStudentInstance = new Student();
  • parameterized constructor :

      Student newStudentInstance = new Student(23,"Freddie Highmore",78f);
    

It can also be created to bind the values passed in the constructor directly to the instance/data variables of the class.

class Student{
            int rollNo;
            String name;
            float marks;

Student(int rollNo , String name , float marks){
    this.rollNo = rollNo;
    this.name = name;
    this.marks = marks;
    }
}

Calling one constructor from another :
calls the below constructor from this constructor -> verify be debugging internally replaces new Student() with new Student(12,"Christina Chang",89.2f);

    Student(){
                this (12,"Christina Chang",89.2f);
}
     Student(int rollNo , String name , float marks){
                this.rollNo = rollNo;
                this.name = name;
                this.marks = marks;
            }

Calls the below constructor from this constructor -> verify be debugging internally replaces new Student() with new Student(12,"Christina Chang",89.2f);

object random is assigned to object anotherRandom, that can still reflect the change made by a random object.

Student random = new Student();
Student anotherRandom = random;

random.name = "Changed Name";
System.out.println(anotherRandom.name);

The behavior of NEW keyword -> allocates memory during runtime [ dynamic memory allocation]. The NEW keyword is not used for primitives, so as to make it faster by storing it in a stack during compilation rather than storing it into HEAP during runtime.

The behavior of this keyword -> capable of replacing every reference variable of the object to assign/initialize values, thus indicating what object you are referring to.

PRIMITIVES are stored in stack & OBJECTS are stored in HEAP.


Kunal Kushwaha Thank you for your DSA boot camp!
Link to the video below