Solved

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

Posted on 2016-07-23
4
78 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
  • 2
4 Comments
 
LVL 15

Assisted Solution

by:gurpsbassi
gurpsbassi earned 125 total points
Comment Utility
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 8

Accepted Solution

by:
Subrat (C++ windows/Linux) earned 250 total points
Comment Utility
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 15

Assisted Solution

by:gurpsbassi
gurpsbassi earned 125 total points
Comment Utility
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 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 125 total points
Comment Utility
Hi rgb192;

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
This video teaches viewers about errors in exception handling.
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now