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

Popular posts from this blog

Control Statements:Selection statement ,Iteration statement and Jump statement in Java

Java Database Connectivity (JDBC) ,Steps to connect to the database in Java

Applets - Inheritance hierarchy for applets, differences between applets and applications, life cycle of an applet, passing parameters to applets, applet security issues.