Write a Java program that creates an object and initializes its data members using constructor. Use constructor overloading concept. rcub
Constructor Overloading :it is a mechanism having more than one constrcutor with diffrent parameters in a same (single) class.
Program :
class student
{
int rno;
String name;
double avg;
public student()//default
{ System.out.println("student is created:");
rno=100;
name="ram ";
avg=89.99;
}
public student( int r,String n)//parameterized
{
rno=r;
name=n;
avg=78.99;
}
public student ( int r ,String n, double a)
{
rno=r;
name=n;
avg=a;
}
public void display()
{
System.out.println("roll no:"+rno);
System.out.println("name:"+name);
System.out.println("average:"+avg);
}
public static void main(String args[])
{
student s=new student();
s.display();
student s1=new student(10,"xyz");
s1.display();
student s3=new student(12,"mno",80.50);
s3.display();
}
}
Comments
Post a Comment