Constructors :-A constructor is a special method that is used to initialize an object.
Rules for creating constructors:
Constructors are two types:
1.Default Constructor:-A constructor that have no parameter is known as default constructor.
class Cons{
Rules for creating constructors:
- Constructor name must be same as its class name.
- Constructor has no return type.
Constructors are two types:
1.Default Constructor:-A constructor that have no parameter is known as default constructor.
class Cons{
int x,y;
Cons(){
x=5;
y=6;
}
public void sum(){
int s=x+y;
System.out.println("Addition of two numbers is :
"+s);
}
public static void main(String args[]){
Cons obj=new Cons();
obj.sum();
}
}
OUTPUT:-
C:\Users\Satyendra\Desktop>javac
1.java
C:\Users\Satyendra\Desktop>java
Cons
Addition of two
numbers is : 11
No comments:
Post a Comment