Displaying Profile Using Java “super” and “this” Keywords

Displaying Profile Using Java “super” and “this” Keywords

Recently, I was learning about Java super and this Keyword. So, I wanted to share this knowledge with you with my small program that explained a lot about using them.

public class CheckingSuper {
public static void main(String args[]) {
Student student = new Student();
student.printIt();
        }
 }
class Person {
public String name = "Saugat Bhattarai";
public void printIt() {
System.out.println("Hello this is me " + name);
    }
}
class Student extends Person {
int age = 21;
String dept = "DoCSE/CE";
int rollNo = 00;
String phoneNo = "9812345678";
String email = "[email protected]";

public void printIt() {
System.out
.println("This is the profile of Kathmandu University Student.");
System.out.println(".....Start of Profile.....");
System.out.println("Name: " + super.name);
System.out.println("Department: " + this.dept);
System.out.println("RollNo: " + this.rollNo);
System.out.println("Age: " + this.age);
System.out.println("phoneNo: " + this.phoneNo);
System.out.println("email " + this.email);
System.out.println(".....End of profile.....");
      }
}

Result:

profile

Comments are closed.