Solved

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

Posted on 2016-07-23
4
89 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
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 8

Accepted Solution

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

Assisted Solution

by:gurpsbassi
gurpsbassi earned 125 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 62

Assisted Solution

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

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

911 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

20 Experts available now in Live!

Get 1:1 Help Now