Site icon DataFlair

Java Program For super and this Keyword

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

Program 1

package testcoreproject;

class Student
{
     int rno;
     String name;
     int per;
     Student(int rno,String name,int per)
     {
       this.rno=rno;
       this.name=name;
       this.per=per;
     }
             
}

class First1
{
    int m=100;  // super class data
    void display()
    {
        System.out.println("This is a display method of Super class");
    }
}
class Second1 extends First1
{
         int m=500;// sub class data
        void show(int m)// method data
       {
            System.out.println("Method Variable: "+m);   
            System.out.println("Current class Variable: "+this.m);
            System.out.println("Super class Variable: "+super.m);
       }
     void display()
    {
        System.out.println("This is a display method of sub class");
        super.display();
        
    } 
}
public class TestSuper1 
{
    public static void main(String[] args) 
    {
        Second1 S=new Second1();
        S.display();
    }
     
}

 

Exit mobile version