Question

I 'discovered' interfaces and I started to love them. The beauty of an interface is that...

I 'discovered' interfaces and I started to love them. The beauty of an interface is that it is a contract, and any object that fulfills that contract can be used wherever that interface is required.

The problem with an interface is that it can't have a default implementation, which is a pain for mundane properties and defeats DRY. This is also good, because it keeps the implementation and the the system decoupled. Inheritance, on the hand, maintains a tighter coupling, and has the potential of breaking encapsulation.

Case 1 ( Inheritance with private members, good encapsulation, tightly coupled)

class Employee
{
int money_earned;
string name;

public:
void do_work(){money_earned++;};
string get_name(return name;);
};


class Nurse : public Employee:
{
   public:
   void do_work(/*do work. Oops, can't update money_earned. Unaware I have to call superclass' do_work()*/);

};

void HireNurse(Nurse *n)
{
   nurse->do_work();
)
Case 2 (just an interface)

class IEmployee
{
     virtual void do_work()=0;
     virtual string get_name()=0;
};

//class Nurse implements IEmployee.
//But now, for each employee, must repeat the get_name() implementation,
//and add a name member string, which breaks DRY.
Case 3: (best of both worlds?)

Similar to Case 1. However, imagine that (hypothetically) C++ did not allow overriding methods except those methods that are pure virtual.

So, in Case 1, overriding do_work() would cause a compile-time error. To fix this, we set do_work() as pure virtual, and add a separate method increment_money_earned(). As an example:

class Employee
{
int money_earned;
string name;

public:
virtual void do_work()=0;
void increment_money_earned(money_earned++;);
string get_name(return name;);
};


class Nurse : public Employee:
{
   public:
   void do_work(/*do work*/ increment_money_earned(); ); .
};
But even this has problems. What if 3 months from now, Joe Coder creates a Doctor Employee, but he forgets to call increment_money_earned() in do_work()?

The question:

Is Case 3 superior to Case 1? Is it because it of 'better encapsulation' or 'more loosely coupled', or some other reason?

Is Case 3 superior to Case 2 because it conforms with DRY?

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
I 'discovered' interfaces and I started to love them. The beauty of an interface is that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • In the Employee class, add an implementation for the calculatePension() method declared in the interface. Calculate...

    In the Employee class, add an implementation for the calculatePension() method declared in the interface. Calculate the pension to date as the equivalent of a monthly salary for every year in service. 4) Print the calculated pension for all employees. ************** I added the interface and I implemented the interface with Employee class but, I don't know how can i call the method in main class ************ import java.io.*; public class ManagerTest { public static void main(String[] args) { InputStreamReader...

  • Need Help ASAP!! Below is my code and i am getting error in (public interface stack)...

    Need Help ASAP!! Below is my code and i am getting error in (public interface stack) and in StackImplementation class. Please help me fix it. Please provide a solution so i can fix the error. thank you.... package mazeGame; import java.io.*; import java.util.*; public class mazeGame {    static String[][]maze;    public static void main(String[] args)    {    maze=new String[30][30];    maze=fillArray("mazefile.txt");    }    public static String[][]fillArray(String file)    {    maze = new String[30][30];       try{...

  • Abstract classes and Interfaces problems 10. Explain one similarity and one difference between ab...

    Abstract classes and Interfaces problems 10. Explain one similarity and one difference between abstract classes and interfaces. 11. Consider the following declarations. public interface Shape{ int someFunction(Shape other); //other functions not shown } public class Square implements Shape {/*implementation not shown*/} Which of the following function headings of someFunction must be added to the declaration of the Square class such that it will satisfy the Shape interface? public int someFunction (Shape other) public int someFunction (Square other) public boolean someFunction(Object...

  • Abstract classes and Interfaces problems 10. Explain one similarity and one difference between abstract classes and...

    Abstract classes and Interfaces problems 10. Explain one similarity and one difference between abstract classes and interfaces. 11. Consider the following declarations. public interface Shape{ int someFunction(Shape other); //other functions not shown } public class Square implements Shape {/*implementation not shown*/} Which of the following function headings of someFunction must be added to the declaration of the Square class such that it will satisfy the Shape interface? public int someFunction (Shape other) public int someFunction (Square other) public boolean someFunction(Object...

  • Greetings, everybody I already started working in this program. I just need someone to help me...

    Greetings, everybody I already started working in this program. I just need someone to help me complete this part of the assignment (my program has 4 parts of code: main.cpp; Student.cpp; Student.h; StudentGrades.cpp; StudentGrades.h) Just Modify only the StudentGrades.h and StudentGrades.cpp files to correct all defect you will need to provide implementation for the copy constructor, assignment operator, and destructor for the StudentGrades class. Here are my files: (1) Main.cpp #include <iostream> #include <string> #include "StudentGrades.h" using namespace std; int...

  • Java program. Code the following interfaces, implement them, and use the interface as a parameter, respectively:...

    Java program. Code the following interfaces, implement them, and use the interface as a parameter, respectively: a. An interface called Printable with a void print() method. b. An interface called EmployeeType with FACULTY and CLASSIFIED integer data. The value of FACULTY is 1 and CLASSIFIED is 2. a. A class called Employee to implement the two interfaces. Its constructor will initialize three instance data as name, employeeType, and salary. Implementation of method print() will display name, employeeType, and salary; salary...

  • Task 3: Main Program Create a main program class that: Creates three or more Nurse instances...

    Task 3: Main Program Create a main program class that: Creates three or more Nurse instances assigned to different shifts. Creates three or more Doctor instances. Creates three or more Patient instances with pre-determined names and manually assigned physicians chosen from the pool of Doctor instances previously created. Generates another 20 Patient instances using randomly generated names and randomly assigns them physicians chosen from the pool of Doctor instances previously created. Prints the toString() values for all employees. Prints the...

  • 1. (TCO 7) Which of the following statements are true? (Points : 5)        Interfaces contain...

    1. (TCO 7) Which of the following statements are true? (Points : 5)        Interfaces contain one and only one implemented method, a constructor.        Interfaces are defined inside an abstract class.        All methods defined in an interface must be implemented when used by another class.        A true object-oriented design must contain as least one interface. Question 2. 2. (TCO 7) In an object-oriented program, methods with no implementation might be found in an _____ and/or a(n) ______....

  • //*Manager.java*// public interface Manager { public void handleCrisis(); } _________________________________________________________ /**Employee.java**/ Public abstract class Employee {...

    //*Manager.java*// public interface Manager { public void handleCrisis(); } _________________________________________________________ /**Employee.java**/ Public abstract class Employee { protected final String name; protected final int id; public Employee(String empName, int empId) { name = empName; id = empId; } public Employee() { name = generatedNames[lastGeneratedId]; id = lastGeneratedId++; } public String getName() { return name; } public int getId() { return id; } //returns true if work was successful. otherwise returns false (crisis) public abstract boolean work(); public String toString() { return...

  • Hey guys I am having trouble getting my table to work with a java GUI if...

    Hey guys I am having trouble getting my table to work with a java GUI if anything can take a look at my code and see where I am going wrong with lines 38-49 it would be greatly appreciated! Under the JTableSortingDemo class is where I am having erorrs in my code! Thanks! :) import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.List; import javax.swing.*; public class JTableSortingDemo extends JFrame{ private JTable table; public JTableSortingDemo(){ super("This is basic sample for your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT