Rahmadnet

inheritance Asigment

Feb 12th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1. package inheritance;
  2.  
  3. class Employee  // employee class as base class
  4. {
  5.     protected double salary = 30000D;
  6.     public Employee()
  7.     {
  8.         System.out.println("This is constructor of Employee class");
  9.     }
  10. }
  11.  
  12. class Developer extends Employee // developer is child class of employee
  13. {
  14.     double bonus = 10000;
  15.     // this constructor will execute after executing employee constructor
  16.     public Developer()
  17.     {
  18.         System.out.println("This is constructor of developer class");
  19.     }
  20.    
  21.     public void displaySalary()
  22.     {
  23.         // the variable 'salary' is in the base class
  24.         System.out.println("The salary is : "+ (salary + bonus) + "\n");
  25.     }
  26. }
  27.  
  28. class Tester extends Employee // Tester is another child class of Employee
  29. {
  30.     double bonus = 20000;
  31.     // this constructor will execute after executing Employee constructor
  32.     public Tester()
  33.     {
  34.         System.out.println("This is constructor of Tester class");
  35.     }
  36.  
  37.     public void displaySalary()
  38.     {
  39.         // the variable 'salary' is in the base class
  40.         System.out.println("The salary is : "+ (salary + bonus)+ "\n");
  41.     }
  42. }
  43.  
  44. package inheritance;
  45.  
  46. public class inheritance
  47. {
  48.     public static void main(String[] args)
  49.     {
  50.         // The constructors of base and derived class will be called respectively
  51.         Developer dev = new Developer();
  52.         dev.displaySalary();
  53.        
  54.         Tester tester = new Tester();
  55.         tester.displaySalary();
  56.     }
  57. }
  58.  
  59. //OUTPUT
  60.  
  61. //This is constructor of Employee class
  62. //This is constructor of developer class
  63. //The salary is : 40000.0
  64. //
  65. //This is constructor of Employee class
  66. //This is constructor of Tester class
  67. //The salary is : 50000.0
Advertisement
Add Comment
Please, Sign In to add comment