class student
{
int grade;
String name;
student() //Default constructor
{
grade = 4;
name = "pqr";
}
void setGrade() //setGrade method
{
grade = grade + 1;
}
void display() //display method
{
System.out.println("Name: "+name);
System.out.println("Grade: "+grade);
}
int getGrade() //getGrade method
{
return grade;
}
}
public class Main
{
public static void main(String[] args) {
student s1 = new student();
//Creating first object
student s2 = new student();
//Creating second object
s1.display(); //Calling display
method on object 1
s2.display(); //Calling display
method on object 2
s2.setGrade(); //Calling setGrade
method on object 2
System.out.println("first object
final grade: "+s1.getGrade()); //Printing the grade for object
1
System.out.println("second object
final grade: "+s2.getGrade()); //Printing the grade for object
2
}
}
Output:-
Get Answers For Free
Most questions answered within 1 hours.