DEV Community

Abhay Prajapati
Abhay Prajapati

Posted on

OOPs Lecture 1

OOPs Lecture 1

learned about contructor's

  • here is what understand about contructor is that;
  • we can create object of a class
class Person{
    String name;
    int age;
}
Enter fullscreen mode Exit fullscreen mode

creating object of class Person

// 
Person p = new Person();
p.name = "John";
p.age = 30;
// so here as we have not created constutor's we need to write p.name; p.age;
Enter fullscreen mode Exit fullscreen mode
class Human {
    String name;
    int age;
    public Human(String name, int age){
        this.name = name;
        this.age = age;
    }
}
Enter fullscreen mode Exit fullscreen mode
Human h = new Human("John", 30);

// as we have created constuctor's we need not to write h.name; h.age;
// else we add that in the parameter of constructor's/fn
// contructors is a special function that is used to initialize the object

Enter fullscreen mode Exit fullscreen mode

pay attention to the this keyword;
it is nothing but the object itself;
this.name == h.name

under the hood the this keyword will be replaced by the object (h) itself

If we donot provide this then we need to always need to write h.name object name;

repo for codeπŸ§‘πŸ»β€πŸ’»

🀝🏾Connect me on:

Twitter: πŸ•ŠοΈ@Abhayprajapati_
Github: 🐧@theabhayprajapati


suggest and editπŸ–‹οΈ

Top comments (0)