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;
}
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;
class Human {
String name;
int age;
public Human(String name, int age){
this.name = name;
this.age = age;
}
}
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
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 writeh.name
object name;
๐ค๐พConnect me on:
Twitter: ๐๏ธ@Abhayprajapati_
Github: ๐ง@theabhayprajapati
Top comments (0)