Solved

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

Posted on 2016-07-23
4
98 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 63

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

778 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