473,386 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

When to use interfaces and when to abstract class ??

308 256MB
When to use interfaces and when to abstract class ??
Sep 10 '20 #1
3 5349
Naheedmir
62 32bit
Abstract classes should be used for closely related objects, whereas interfaces are used for providing common functionality to unrelated classes. Use interfaces for designing small functional units. However, use abstract class if you are designing large functional units
Sep 10 '20 #2
AjayGohil
83 64KB
Hy,

An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.

Refer this site for more information:

https://www.tutorialspoint.com/when-...erface-in-java
Sep 16 '20 #3
Joseph Martell
198 Expert 128KB
This took me a while to wrap my head around this and ultimately I just had to write code that utilized both to figure it out.

An abstract class is an inheritance based concept. It should satisfy the "is a" rule (checkout Liskov Substitution Principle). As an example:
Expand|Select|Wrap|Line Numbers
  1.     public abstract class Dog
  2.     {
  3.         public abstract void Bark();
  4.     }
  5.  
  6.     public class Bulldog
  7.         : Dog
  8.     {
  9.         public override void Bark()
  10.         {
  11.             Console.WriteLine("Woof");
  12.         }
  13.     }
  14.  
  15.     public class Chihuahua
  16.         : Dog
  17.     {
  18.         public override void Bark()
  19.         {
  20.             Console.WriteLine("yip");
  21.         }
  22.     }
  23.  
A bulldog is a dog. A chihuahua is a dog. This is super important when talking about inheritance because you are building a representation of a mental (or domain) model. It makes sense that bulldog is a dog. It would NOT make sense for a class named PeatMoss to inherit from the Dog abstract class, even if you needed Peat Moss to have some method named "Bark". Peat Moss is not a Dog.

Interfaces are a very different construct. Interfaces guarantee that an object, whatever that object is, can perform some sort of action. A class that implements IComparable can be compared (CompareTo method). It is not an object of type "comparable". If we implement IEnumerable our objects must create an enumerator to walk a collection. We have not created an object of type "Enumerable". We have just guaranteed a behavior without layering on any other notions about what our object is. (Dogs could implement IComparable as could PeatMoss without any dissonance in our domain/mental model).

Abstract classes are about inheritance hierarchies and the domain model you are working in. Interfaces deal with specific, constrained behaviors you need a class to have.
Nov 9 '20 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Joe | last post by:
My question is more an OOD question. I know *how* to implement both abstract classes and interfaces. Here's my question - under what circumstacnes does one use an abstract class and under what...
18
by: Bradley | last post by:
I'm trying to determine if there's a general rule for when an Interface should used vs. an Abstract Class. Is there any design advantage to using one or the other? Brad
17
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit...
7
by: ankitjain.bvcoe | last post by:
Hi i have the following problem in my design :::: i want to define an abstract class LogBuffer and derive two singleton classes from it i.e AlarmBuffer and FireWallBuffer.For this my design is...
2
by: tony | last post by:
Hello! I'm trying to build a class library which has a class called AvestaPlantFunc. In this project building a class libray exist a class called AvestaPlantFunc. In this class is there a...
6
by: Tony Johansson | last post by:
Hello!! If you write private class Test {} you have a private class If you write public class Test {} you have a public class If you write protected class Test {} you have a protected class If...
9
by: silversurfer2025 | last post by:
Hello everyone, I am currently having problems with a C++ abstract class. I have a class FrameWork.h which defines some methods (of which some are abstract, i.e. virtual void method() = 0). In...
3
by: psbasha | last post by:
Hi, Could anybody help me,whether we have interface and abstract class concepts available in Python. If so,could you please provide me a sample piece of code for client and server side ...
5
by: tshad | last post by:
In VS 2003, I am setting up an abstract class that is setting up classes for each datatype of VB.Net (as well as C#). I am trying to set it up so that most of the work is done in the Abstract...
2
by: aswal | last post by:
Both of them provides abstraction and we have to give their implementation in their child classes so what's exactly is the difference between them and also please suggest me some scenario where i can...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.