1) { } By using the “extend” keyword

 

1)
What is the relationship between the Employee class and the Manager class? What
concept does this demonstrate?

A)The relationship between Employee class
and Manager class is Inheritance(Single Inheritance).  In this relationship the class which inherits
the properties from other class is called sub class , where as the class whose
properties are inherited to other class is called sub class. Here the Employee
class is Super class(parent) and the Manager class is a sub class(child) to the
Employee class.

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!


order now

 

public class Manager extends Employee

{

     

}

 

By
using the “extend” keyword Manger class inherits the properties of Employee
class. It represents that Manager class can have the methods and instances of Employee
class, but Manger class cannot access the myTitle and myAge of Employee class
because they are declared as private. Whereas the Manager class can access the
mySalary of Employee class.

2)
What would happen if mySalary were declared private instead of protected?

A)
If any Variable or a method is declared as private (Access specifier), then
that variable or method can be accessed and modified with in the same class but
not by other classes. If variable is declared as protected, it can be accessed from
the other classes with in the same package.

So
if the mySalary is declared as private instead of protected in Employee class,
it can not be accessed by override method public void raiseSalary(int percent);
in the Manager class, because private members cannot be accessed from other
classes. If we try to access it, the program shows the errors. So this is the
reason it is declared as protected.

 

 

Example
code:

Class Employee

{

private double mySalary;

}

class Manager extends
Employee

{

public void raiseSalary()

{

 

//This function cannot use
mySalary;  as it is declared as private
in Employee class

}

}

 

3) Write the output for each section of print
statements.

A)    

Name:
John Doe

Title:
Department Head

Age
: 54

Years
of Experience : 31

Highest
Degree Earned : PhD

Salary
: 100000.0

Promotion
Bonus : 5000.0

Salary
Raised!

New
Salary : 114500.0

 

 

Name
: Jane Doe

Title
: Resource Analyst

Age
: 41

Salary
: 75000.0

Salary
Raised

New
Salary : 82125.0

Name
: Jim Doe

Title
: Engineer

Age
: 45

Salary
: 85000.0

Salary
Raised

New
Salary : 93925.0

 

4)
Which raiseSalary() method does m.raiseSalary(10) execute? Why? What concept
does this demonstrate?

A)
m.raiseSalary(10) executes method public void raiseSalary(int percent); present
in the Manager class, as there is a inheritance between Manager class and
Employee class the super class method is override by the sub class method. This
concept demonstrates method overriding(Polymorphism).  It represents, the child class should have
same method as declared in the parent class.

Rules for method Overriding

1.    Child class method should have same name as
parent class method

2.    Child class method should have same number and
type of parameters as parent class method

3.    There should be a Is-A relationship.(inheritance).

This concept demonstates the runtime polymorphism. JVM
decides which method has to be executed at runtime.

5)
Which raiseSalary() method does e1.raiseSalary(10) execute? Why? What concept
does this demonstrate?

A)e1.raiseSalary(10)
method executes the  raiseSalary(int
percent) method  present in Employee
class, because e1 is the object of Employee class. Employee class also contains
raiseSalary(int percent, int cost_of_living_adjustment) method but this method
contains two integer arguments, but in e1.raiseSalary(10) method has only one
argument, so compiler calls only raiseSalary(int percent) method present in the
Employee class. This concept is called static polymorphism, because call is
resolved by the compiler.

 

 

 

Example

Class Employee

{

public void raiseSalary(int percent)

{

         //raiseSalary
with one integer argumnet

}

 public void raiseSalary(int percent, int cost_of_living_adjustment)

{

 

         //raiseSalary
with two integer argumnet

 

}

 

Class Test

{

         e.raiseSalary(10) // calls raiseSalary() method with one
integer argumnet

}

 

6)
Which raiseSalary() method does e2.raiseSalary(10,1) execute? Why? What concept
does this demonstrate?

A)
e1.raiseSalary(10,1) method executes the method raiseSalary(int percent, int
cost_of_living_adjustment)  present in
Employee class. This is because the e1 object here refers to Employee class.

Even though the Employee class has an overloaded rmethod raiseSalary(int
percent), we need to pass only one integer argument to call that method, but
here we are passing two integer arguments. So, the compiler calls raiseSalary(int
percent, int cost_of_living_adjustment)method in Employee class. This
demonstrates the concept of static/compile time polymorphism.

 

 

 

Example

Class Employee

{

public void raiseSalary(int percent)

{

         //raiseSalary
with one integer argumnet

}

 public void raiseSalary(int percent, int cost_of_living_adjustment)

{

 

         //raiseSalary
with two integer argumnet

 

}

}

 

Class Test

{

         e.raiseSalary(10,1) // calls raiseSalary() method with two
integer arguments

}