Constructor:
A constructor in Java is a square of code like a technique
that is called when an occasion of a question is made. Here are the key
contrasts between a constructor and a strategy: A constructor doesn't have an arrival
compose. ... Not at all like strategies, constructors are not thought about
individuals from a class.
Types of Constructor
There are two sort of constructor in Java:
- No-argument constructor
- Parameterized Constructor
No-contention constructor: A constructor that has no parameter is known as default constructor. In the event that we don't characterize a constructor in a class, at that point compiler makes default constructor(with no contentions) for the class. Furthermore, in the event that we compose a constructor with contentions or no-contention then compiler does not make default constructor.
Parameterized Constructor: A constructor that has parameters is known as parameterized constructor. In the event that we need to introduce fields of the class with your own qualities, at that point utilize parameterized constructor.
Syntax:
public human() {
this.age=40;
}
What is this?
It will be discussed in lecture 8 after the concept of
inheritance in lecture 6
Why we use constructor?
Constructor is basically used to perform those tasks which
we want to perform when the object is created as the code in it executes when
the object is created.
Program:
package studywebsite;
public class human
{
public int age;
public human() {
this.age=40;
}
public void age()
{
System.out.println("AGE is "+this.age);
}
public static void
main(String[] args)
{
human obj=new
human();
obj.age();
}
}
Next lecture Previous Lecture
0 Comments
Your Comment is Submitted