?
Solved

why is it wrong to combine interface and abstract class; acquire the method externally?

Posted on 2016-07-23
4
Medium Priority
?
168 Views
Last Modified: 2016-08-08
interface printer{
  printToScreen(){}
  printToPrinter(){}
 printToCellPhone(){}
 sendToFax(){}
}



computer implements printer
cableBox tv box implements printer
cable router implements router


all 3 of these classes must print
may be easier to create abstract class than to keep on creating new methods and copy pasting those methods into all classes

abstract class AllPrintingMehods(){
 printToScreen(){}
  printToPrinter(){}
 printToCellPhone(){}
 sendToFax(){}

}




why is it wrong to combine interface and abstract class

why is wrong to acquire the method externally?
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 16

Assisted Solution

by:gurpsbassi
gurpsbassi earned 500 total points
ID: 41726062
you have to think about what your desired architecture is.

You can use abstract classes where there is a true "is-a" relationship modelled. Interfaces are normally used to define contracts/behaviour where there is a "can-do" relationship hence most interfaces in the java api are defined with the ending "*able" e.g. serializable,  Cloneable.
0
 
LVL 9

Accepted Solution

by:
Subrat (C++ windows/Linux) earned 1000 total points
ID: 41726221
Interface never have any definition, only having pure virtual functions. As already told "can-do"

But abstract class is having atleast one pure virtual and other may or mayn't be . We need it to restrict creation of object.

Again, if know some functions are reusable by derivatives, and some are need specific implementation, then Abstract class is the case.

If all fuctions are abstract, then ur interface.
0
 
LVL 16

Assisted Solution

by:gurpsbassi
gurpsbassi earned 500 total points
ID: 41726223
Again, if know some functions are reusable by derivatives, and some are need specific implementation, then Abstract class is the case.

If all fuctions are abstract, then ur interface.

this should not be the deciding factor for going for abstract class v interface.

Just putting functionality into a abstract class for reuse is not a good pattern to follow. You end up with a class with a bunch of meaningless  methods which could easily be replaced with a class containing static methods.
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 500 total points
ID: 41726543
Hi rgb192;

Have a look at this Microsoft documentation, Recommendations for Abstract Classes vs. Interfaces.
0

Featured Post

Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question