Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package inheritance;
- class Employee // employee class as base class
- {
- protected double salary = 30000D;
- public Employee()
- {
- System.out.println("This is constructor of Employee class");
- }
- }
- class Developer extends Employee // developer is child class of employee
- {
- double bonus = 10000;
- // this constructor will execute after executing employee constructor
- public Developer()
- {
- System.out.println("This is constructor of developer class");
- }
- public void displaySalary()
- {
- // the variable 'salary' is in the base class
- System.out.println("The salary is : "+ (salary + bonus) + "\n");
- }
- }
- class Tester extends Employee // Tester is another child class of Employee
- {
- double bonus = 20000;
- // this constructor will execute after executing Employee constructor
- public Tester()
- {
- System.out.println("This is constructor of Tester class");
- }
- public void displaySalary()
- {
- // the variable 'salary' is in the base class
- System.out.println("The salary is : "+ (salary + bonus)+ "\n");
- }
- }
- package inheritance;
- public class inheritance
- {
- public static void main(String[] args)
- {
- // The constructors of base and derived class will be called respectively
- Developer dev = new Developer();
- dev.displaySalary();
- Tester tester = new Tester();
- tester.displaySalary();
- }
- }
- //OUTPUT
- //This is constructor of Employee class
- //This is constructor of developer class
- //The salary is : 40000.0
- //
- //This is constructor of Employee class
- //This is constructor of Tester class
- //The salary is : 50000.0
Advertisement
Add Comment
Please, Sign In to add comment