The tag has no usage guidance.

learn more… | top users | synonyms

55
votes
7answers
55k views

When to use abstract classes instead of interfaces with extension methods in C#?

"Abstract class" and "interface" are similar concepts, with interface being the more abstract of the two. One differentiating factor is that abstract classes provide method implementations for derived ...
29
votes
9answers
59k views

Why should I declare a class as an abstract class?

I know the syntax, rules applied to abstract class and I want know usage of an abstract class Abstract class can not be instantiated directly but can be extended by other class What is the ...
18
votes
4answers
15k views

In C++ why and how are virtual functions slower?

Can anyone explain in detail, how exactly the virtual table works and what pointers are associated when virtual functions are called. If they are actually slower, can you show the time that the ...
17
votes
5answers
4k views

Do enums create brittle interfaces?

Consider the example below. Any change to the ColorChoice enum affects all IWindowColor subclasses. Do enums tend to cause brittle interfaces? Is there something better than an enum to allow for ...
16
votes
2answers
2k views

Abstract exception super type

If throwing System.Exception is considered so bad, why wasn't Exception made abstract in the first place? That way, it would not be possible to call: throw new Exception("Error occurred."); This ...
13
votes
7answers
11k views

What are the roles of singletons, abstract classes and interfaces?

I am studying OOP in C++ and, even though I am aware of the definitions of these 3 concepts, I cannot really realize when or how to use it. Let's use this class for the example: class Person{ ...
13
votes
4answers
1k views

Is there a different usage rationale for abstract classes/interfaces in C++ and Java

According to Herb Sutter one should prefer abstract interfaces (all pure virtual functions) to abstract classes in C++ to decouple the implementation as far as possible. While I personally find this ...
12
votes
3answers
4k views

Should I implement an interface directly or have the superclass do it?

Is there a difference between public class A extends AbstractB implements C {...} versus... public class A extends AbstractB {...} abstract class AbstractB implements C {...} I understand that ...
11
votes
6answers
9k views

What are the differences between abstract classes, interfaces, and when to use them

Recently I have started to wrap my head around OOP, and I am now to the point where the more I read about the differences between abstract classes and interfaces the more confused I become. So far, ...
11
votes
5answers
6k views

Should all public methods in an abstract class be marked virtual?

I recently had to update an abstract base class on some OSS that I was using so that it was more testable by making them virtual (I could not use an interface as it combined two). This got me thinking ...
10
votes
4answers
9k views

Can I consider interface methods as abstract methods?

I was thinking about that, and I had some doubts. When I declare an interface, for example: public interface MyInterface { public void method1(); public void method2(); } Could these ...
8
votes
3answers
921 views

Refactoring an existing abstract class and its parameters

I have an abstract class A which declares an abstract method doStuff. Currently there are many classes that inherit from A and implement doStuff. The class' instances are initialized at run-time ...
7
votes
4answers
7k views

Abstract methods vs instance variables for reusable objects

I have quite a bit of Java code that I'm re-working to be re-used. The problem is that there are many pieces that are project specific so there are ends up being a higher level of coupling between ...
6
votes
6answers
8k views

Is there any difference between interfaces and abstract classes that have abstract methods only?

Let's say we have an abstract class and let this class has only abstract methods. Is this abstract class different from an interface that has same methods only? What I am looking to know is if there ...
6
votes
3answers
6k views

Abstract base classes and copy construction, rules of thumb

Often times it's a good idea to have an abstract base class to isolate the interface of the object. The problem is that copy construction, IMHO, is pretty much broken by default in C++, with copy ...
6
votes
2answers
659 views

Help to understand the abstract factory pattern

I'm learning the 23 design patterns of the GoF. I think I've found a way to understand and simplify how the Abstract Factory works but I would like to know if this is a correct assumption or if I am ...
5
votes
6answers
6k views

What are abstract classes and abstract methods?

I got several explanations but so far I'm not able to understand that what are the abstract classes and methods in Java. Some said it has to do something with the security of the program, other said ...
5
votes
3answers
690 views

Switching from abstract class to interface

I have an abstract class which has all abstract methods except one which constructs objects of the subclasses. Now my mentor asked me to move this abstract class to an interface. Having an interface ...
5
votes
2answers
232 views

Is it good design to put abstract methods in an abstract class even though the methods are already defined in its interface?

Let's say we have an interface Foo with a method "public void someMethod()", and we have one or multiple abstract classes that "implement" Foo but don't necessarily themselves provide an ...
5
votes
1answer
112 views

Inheritance from children to parents?

Seeing this UML class diagram : FileHandler, Uploader and Deleter are abstract classes. The four bottom classes are implementations. They extend on FileHandlerInterface which requires a handle ...
4
votes
4answers
13k views

Abstract class + Inheritance vs Interface

Hello fellow programmers, I am reading a book on C# and the author is comparing Abstract classes and Interfaces. He claims that if you have the following "abstract class:" abstract class ...
4
votes
2answers
258 views

Why is an abstract class forced to define all functions of an inherited interface?

Why does c# compiler force an abstract class to define (as opposed to implement) all the methods of the inherited interface ? why does it not mark unimplemented methods of an inherited interface as ...
4
votes
4answers
1k views

Which methods should be put in an interface and which in abstract classes?

I have seen many frameworks and modules and their standard they follow is like this UserInterface which have some predefined methods AbstractUserClass which implements userInterface Then ...
4
votes
3answers
286 views

Using abstract methods to force subclasses to define values for member fields

Often in my designs I define an abstract superclass whose subclasses will vary mostly in their values for the fields defined in the superclass. For example, in a game I'm developing there's an ...
4
votes
1answer
319 views

Liskov substitution principle with abstract parent class

Does Liskov substitution principle apply to inheritance hierarchies where the parent is an abstract class the same way if the parent is a concrete class? The Wikipedia page list several conditions ...
3
votes
2answers
117 views

Is there any well known concern about having abstract class with few abstract methods? [closed]

I've been asked to explain why it may be bad to have an abstract class that implements most of the methods (as an extreme case, only one abstract method) and has many sub-classes. Apart from having ...
3
votes
1answer
115 views

Skeletal/Abstract class just to store shared state?

Given an interface for the Strategy pattern like so: public interface Strategy { public Output execute(Input input); public Output getLastExecutionOutput(); } if I want to store the last ...
3
votes
2answers
8k views

Building a database class in PHP

I wonder if I should write a database class for my application, and if so, how to accomplish it? Over there on SO, a guy mentioned it should be written as an abstract class. However, I can't ...
2
votes
6answers
628 views

What is the best design decision approach?

I have two classes (named MyFoo1 and MyFoo2) that share some common functionality. So far, it does not seem like I need any polymorphic inheritance but, at this point, I am considering the following ...
2
votes
2answers
783 views

Liskov substitution and abstract classes / strategy pattern

I'm trying to follow LSP in practical programming. And I wonder if different constructors of subclasses violate it. It would be great to hear an explanation instead of just yes/no. Thanks much! P.S. ...
2
votes
3answers
175 views

When and why would you extract an abstract method from an abstract class to an interface?

I have been asked the following question in an interview: "What is the need of an interface when you can have an abstract method within an abstract class?" Which I did not know the answer to. Could ...
2
votes
2answers
246 views

How much implementation should I provide in an abstract class? [closed]

I have an abstract class AbstractService and about 10 classes that extend from AbstractService. However, I'm not sure how many methods and what methods I should implement in the abstract class and how ...
2
votes
1answer
91 views

How valuable is decoupling in proprietary software?

When developing with Symfony2 PHP framework I often see reccomendations to decouple code but most of the time that is accompanied by a 'if you want to release as a third party bundle'. Which begs the ...
2
votes
2answers
663 views

Abstract Property In Base Class To Force Programmer To Define It

I'm coding with a state pattern for an embedded device. I have a base/abstract class called State and then each discrete(concrete) state class implements the abstract State Class. In the State Class ...
2
votes
2answers
443 views

Abstract classes and constants

I have two classes that share a lot of code and are conceptually variations on a common class. So I want an abstract class that contains all their common code and then they can each inherit from it ...
2
votes
2answers
303 views

observer class as instance

Sometimes I read in observer-pattern descriptions, to make the constructor of a observer base class protected so the class will be abstract. but by making the constructor public (if even one is ...
2
votes
2answers
738 views

Questions about Polymorphism

So I understand the importance of Polymorphism, including how vital it is. But something I don't quite understand is what about the Constructor and any inherited Class the initial Base Class may have....
2
votes
2answers
748 views

Testing abstract class' behavior

I'm currently refactoring an existing design, which was created without TDD. There is a class hierarchy with one abstract base class and two subclasses. In the original design, these classes were ...
2
votes
1answer
177 views

Creating multiple instances of an implementing class

I currently have an abstract class and multiple classes extending it. My problem is, that there has to be a way to create a variable number of instances of an extending class. The number of instances ...
1
vote
3answers
2k views

Force Derived Class to Implement Static Method C#

So the situation is like, I have few classes, all of which have a standard CRUD methods but static. I want to create a base class which will be inherited so that it can force to implement this CRUD ...
1
vote
3answers
306 views

How can I sell DRY? [closed]

Where I work we have around 10 VS projects in a solution which are identical in functionality (with some different different rules in a few methods) and share many exact methods. They share the same ...
1
vote
5answers
833 views

Should abstract classes be used to prevent instantiation

Is preventing the instantiation of a class a valid reason to make it abstract? For example, if I have a class called Gauge which has subclasses HeadingIndicator and AirSpeedIndicator, is it a good ...
1
vote
3answers
44 views

How to represent the following scenario in a class diagram?

Consider a store with multiple branches and a system generates reports for each branch monthly and annually and an overall report as well monthly and annually. So in total there are 4 types of ...
1
vote
1answer
679 views

Interface Dependencies or Abstract Classes

I have a decision to make and I am wondering what would be the better solution. I am refactoring an older application and intend to really get into the nuts and bolts of it. There are 8 report types ...
1
vote
2answers
178 views

Should an abstract class always abstract its methods to an interface?

I had learned sometime ago that abstract classes should always abstract their functions to an interface. So instead of having something like this - abstract class a{ public int i; public int ...
1
vote
1answer
230 views

Names for generic classes - how to avoid naming them just “object”? Use synonyms?

When programming, I often end up naming some generic class (think of an abstract base class for stuff you manage in your project) something like FooObject, FooComponent or FooItem (where Foo is an ...
1
vote
2answers
3k views

What other reasons are there to write interfaces rather than abstract classes? [duplicate]

Possible Duplicate: When to use abstract classes instead of interfaces and extension methods in C#? When I read and looked at codes using Abstract classes, I was able to justify it because it ...
1
vote
2answers
242 views

Convert this abstract class to an interface

I have a security method I would like to be able to sprinkle into other classes throughout my program. It is currently an abstract class but I feel it would be more appropriate as an interface. Can ...
1
vote
1answer
445 views

Designing Subclasses for an abstract base class in C++

I'm doing a small assignment in C++ where I have to use concepts like virtual functions and inheritance in a domain of my choice. I'm implementing the prototype for a package manager in Linux. I have ...
1
vote
1answer
111 views

Pure abstract classes for input-, output- and bidirectional-pin of microcontroller

I'm writing a real-time operating system for microcontrollers in C++11 - distortos. Currently I'm thinking about a C++ framework for various peripherals. The most basic peripheral which I would like ...