package testpacknm; import java.util.Scanner; import testpacknm.testcnm; class Another { String name; } public class testcnm extends Another { int salary; public static void main(String[] args){ testcnm obj = new testcnm(); obj.salary = 10000; obj.name = "Prakash"; System.out.println("Name: " + obj.name); System.out.println("Salary: " + obj.salary); } }
2
package testpacknm; import java.util.Scanner; import testpacknm.testcnm; class Salary extends Details { public void SalaryMethod() { System.out.println("Calling the salary method"); } } class Details { public void detailMethod() { System.out.println("Calling detail method"); } } public class testcnm { public static void main(String[] args) { Salary sal = new Salary(); sal.detailMethod(); sal.SalaryMethod(); } }
3
package testpacknm; import java.util.Scanner; import testpacknm.testcnm; public class testcnm { public static void main(String[] args) { Salary sal = new Salary(); sal.detailMethod(); sal.SalaryMethod(); sal.data(); } } class Salary extends Details { public void SalaryMethod() { System.out.println("Calling the salary method"); } } class Details extends Database { public void detailMethod() { System.out.println("Calling detail method"); } } class Database { public void data() { System.out.println("Callind database method"); } }
4
/** * Written for 'JavaDump' Android Application */ // Hierarchical Inheritance Example public class Employee { public static void main(String[] args) { Salary sal = new Salary(); // sal.detailMethod(); //throws error sal.SalaryMethod(); sal.data(); } } class Salary extends Database { public void SalaryMethod() { System.out.println("Calling the salary method"); } } class Details extends Database { public void detailMethod() { System.out.println("Calling detail method"); } } class Database { public void data() { System.out.println("Callind database method"); } } OUTPUT: Calling the salary method Callind database method