- import java.io.*;
- class student {
- static int count;//Does not Require initialization.
- int count1 = 0;//Non Static Member Variable must to be initialized
- student() {
- acc_details();//Accessed only within the class
- }
- public static void stu_details()//static method can be called without the object
- {
- System.out.println("Name:XXXX");
- System.out.println("Roll No:123456");
- count++;
- //count1++;here it cannot be used because non static member could not be used in the static function
- }
- private void acc_details()//non static method can be called only with the object
- {
- System.out.println("Entered into Private Access Specifiers");
- System.out.println("Password:*******");
- count++;
- count1++;
- }
- protected void att_details()//non static method can be called only with the object
- {
- System.out.println("Attendance Details");
- count++;
- count1++;
- }
- }
- class staff extends student {
- protected static void sta_details()//static method can be called without the object
- {
- count++;
- //count1++;here it cannot be used because non static member could not be used in the static function
- System.out.println("Name:YYYY");
- }
- }
- class hod extends staff {
- protected static void hod_details()//static method can be called without the object
- {
- count++;
- //count1++;here it cannot be used because non static member could not be used in the static function
- System.out.println("Name:ZZZ");
- }
- }
- class college extends hod {
- public static void main(String a[]) {
- stu_details();//College can view the student details because it is in public mode
- /*
- * static method can be called without the object .If you didnt specify
- * as static during the declaration of this function you will get an
- * error message during the calling of this function
- */
- sta_details();//College can view the staff details because it is in public mode
- /*
- * static method can be called without the object .If you didnt specify
- * as static during the declaration of this function you will get an
- * error message during the calling of this function
- */
- hod_details();//College can view the hod details because it is in public mode
- /*
- * static method can be called without the object .If you didnt specify
- * as static during the declaration of this function you will get an
- * error message during the calling of this function
- */
- staff s1 = new staff();
- s1.stu_details();//staff can also view the student details because it is in public mode
- hod s2 = new hod();
- s2.stu_details();//hod can also view the student details because it is in public mode
- s1.att_details();//staff can also view the student attendance details because it is inherited so it has an access over protected details.
- s2.att_details();//staff can also view the student attendance details because it is inherited so it has an access over protected details.
- //acc_details() cannot not be viewed by any of the classes like staff,hod and college becuase it is in private mode.
- student s = new student();
- //s.acc_details(); it cannot be called because private mode function only accessed within the function.
- s.stu_details();
- s.att_details();
- System.out.println("Count value without object:" + count);//count variable can be called without an object
- //System.out.println("Count1 Value:" + count1); count1 variable cannot be called without an object because it is non-static
- System.out.println("Count value with object of class student:" + s.count);
- System.out.println("Count value with object of class staff:" + s1.count);
- System.out.println("Count value with object of class hod:" + s2.count);
- System.out.println("Count1 value with object of class student:" + s.count1);
- System.out.println("Count1 value with object of class staff:" + s1.count1);
- System.out.println("Count1 value with object of class hod:" + s2.count1);
- }
- }