Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Two of the main object-oriented features of the Java programming language are the ability to inherit the code of one class, and the ability to

Two of the main object-oriented features of the Java programming language are the ability to inherit the code of one class, and the ability to override some of the inherited methods to affect a polymorphic behavior. In this discussion, you will explore both of these concepts and how should they be programmed.

Examine the loaded project, and answer these questions:

  • What would be the output of this program when it is run?
  • Why does calling toString() on the Person and Student objects return their names ("Person" and "Student") while calling toString() on the Employee object returns "Person"?
  • How can we fix this such that calling toString() on the Employee object return its name as well?

Provide a screenshot of your result of your fix.

PLEASE Explain, briefly, how you completed this exercise, the algorithm you used (via pseudo code or other description tools), the major issues you faced. and how you solved these issues.

CODE BELOW:

package u1d1_extendoverride;

public class U1D1_ExtendOverride {

/** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here System.out.printf( "toString() on Person returns => \t%s ", new Person().toString()); System.out.printf( "toString() on Student returns => \t%s ", new Student().toString()); System.out.printf( "toString() on Employee returns => \t%s ", new Employee().toString()); } }

class Person { protected String name; protected String address; protected String phoneNumber; protected String email;

public String toString() { return "Person"; } }

class Student extends Person { public static int FRESHMAN = 1; public static int SOPHOMORE = 2; public static int JUNIOR = 3; public static int SENIOR = 4;

protected int status;

public String toString() { return "Student"; } }

class Employee extends Person { protected String office; protected int salary; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

how to record general reserve in cash flow statement

Answered: 1 week ago