filmov
tv
#4.2 Constructor in Java Theory
Показать описание
In this lecture we are discussing:
1)Know some terminology and term uses in java
2)What we need to call any method in java ?
3)What is Constructor ?
4)Use of constructor?
5)Type of constructor and glance of constructor overloading?
#1
key terminology :
i) return type int, float, void
ii) access specifiers public, private, default, protected--- we will talk this on special lecture
iii) Constructor --- particularly this topic discuss in lecture.
iv) static and main
#2
What we need to call any method in java?
:-- We need a object to call any method
:-- then question arise how we call main method without object, from main our program not started
for starting program we need object and for creating object we need some space to create object. If this happen we cannot run our program.
:--To solve this we make main method as static .It means for static method we do not require object to call.
:--JVM call main method without creating object.
#3
What is Constructor?
It is special member method of object. We can say it is special type of setter (to set the value of instance variable) used to set
value of instance variable. It is called when we create our object and it has no any return type.
e.g
class A{
int a;
A(){ //non parametrized constructor
//if you don’t declare constructor by default constructor available for every class
a=5; //initialize 5 as default value of i
}
public void wish(){
}
}
public class Main{
public static void main(String []args){
A obj=new A(); // obj is reference variable
// new A() is used to create object
//with creation of object automatically call constructor of A class
}
}
#4
Use of Constructor
-- constructor is used to create object
-- Constructor is used to allocate memory in heap memory.
-- Work as seater (to seat value of instance variable)
-- constructor overloading (we shall talk in other lecture)
-- constructor chaining (After learning this() and super() we can discuss)
#5
Type of Constructor
i) default constructor (this is also not parametrized constructor but when we not write constructor then by default jvm create)
ii) parametrized constructor
iii) non parametrized constructor
e.g
class A{
int a;
A(){
//non parametrized constructor
}
A(int i){
a=i; //parametrized constructor
}
}
class B{
//in this case jvm provide by default non parametrized constructor
}
Note:
i)When we use multiple constructor in same class then it is known as constructor overloading.
we are done this in class A. Right now we are not study polymorphism that’s why we are not talk more about constructor overloading.
ii) When we will study polymorphism we can talk about method overloading. Which will be same as a constructor overloading.
This video shows what is constructor. How to use constructor?
What are different types of constructor.
Check out our courses:
Coupon: TELUSKO10 (10% Discount)
More Learning :
1)Know some terminology and term uses in java
2)What we need to call any method in java ?
3)What is Constructor ?
4)Use of constructor?
5)Type of constructor and glance of constructor overloading?
#1
key terminology :
i) return type int, float, void
ii) access specifiers public, private, default, protected--- we will talk this on special lecture
iii) Constructor --- particularly this topic discuss in lecture.
iv) static and main
#2
What we need to call any method in java?
:-- We need a object to call any method
:-- then question arise how we call main method without object, from main our program not started
for starting program we need object and for creating object we need some space to create object. If this happen we cannot run our program.
:--To solve this we make main method as static .It means for static method we do not require object to call.
:--JVM call main method without creating object.
#3
What is Constructor?
It is special member method of object. We can say it is special type of setter (to set the value of instance variable) used to set
value of instance variable. It is called when we create our object and it has no any return type.
e.g
class A{
int a;
A(){ //non parametrized constructor
//if you don’t declare constructor by default constructor available for every class
a=5; //initialize 5 as default value of i
}
public void wish(){
}
}
public class Main{
public static void main(String []args){
A obj=new A(); // obj is reference variable
// new A() is used to create object
//with creation of object automatically call constructor of A class
}
}
#4
Use of Constructor
-- constructor is used to create object
-- Constructor is used to allocate memory in heap memory.
-- Work as seater (to seat value of instance variable)
-- constructor overloading (we shall talk in other lecture)
-- constructor chaining (After learning this() and super() we can discuss)
#5
Type of Constructor
i) default constructor (this is also not parametrized constructor but when we not write constructor then by default jvm create)
ii) parametrized constructor
iii) non parametrized constructor
e.g
class A{
int a;
A(){
//non parametrized constructor
}
A(int i){
a=i; //parametrized constructor
}
}
class B{
//in this case jvm provide by default non parametrized constructor
}
Note:
i)When we use multiple constructor in same class then it is known as constructor overloading.
we are done this in class A. Right now we are not study polymorphism that’s why we are not talk more about constructor overloading.
ii) When we will study polymorphism we can talk about method overloading. Which will be same as a constructor overloading.
This video shows what is constructor. How to use constructor?
What are different types of constructor.
Check out our courses:
Coupon: TELUSKO10 (10% Discount)
More Learning :
Комментарии