Sunday, 13 March 2016

How to create parameterized constructors in java


2.Parameterized constructor:-A constructor that hava parameters is known as parameterized constructor.
 
class Cons{
int x,y;
Cons(int a, int b){
x=a;
y=b;
}
public void mult(){
int m=x*y;
System.out.println("Multiplication of two numbers is "+m);
}
public static void main(String args[]){
Cons obj=new Cons(5,4);
obj.mult();
}
}

OUTPUT:- 
C:\Users\Satyendra\Desktop>javac 1.java
C:\Users\Satyendra\Desktop>java Cons
Multiplication of two numbers is 20

No comments:

Post a Comment