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 are any differences both philosophically, objectively and in the underlying programming language implementation between an Abstract Class with only abstract members and an equivalent Interface?

3

Technically, the differences aren’t really significant but, conceptually, they are entirely different things and that leads to the technical differences others have mentioned.

An abstract superclass is exactly what it sounds like, it’s a common type that is shared by many other types, like Cats and Dogs are Animals.

An interface is also exactly what it sounds like, it’s an interface through which other classes can communicate with the object. If you want to make a Cat Walk, you’re ok, cause Cat implements a CanWalk interface. Same for a Lizard, though they walk very differently. A Snake, on the other hand, does not implement CanWalk, so you can’t tell it to Walk. Meanwhile, Lizard and Snake (or possibly more explicit subclasses — I’m not an expert) might both shed their skin, and thus implement CanShed, while a Cat couldn’t do that.

But they’re all still Animals and have some common properties, like whether they’re alive or dead.

This is why all methods on an interface must be implemented as public (or explicitly, in C#). Cause what’s the point in an interface that’s hidden from the class interfacing with the object? It’s also why you can have multiple interfaces to an object, even when a language doesn’t support multiple inheritance.

To go back to your question, when you look at it this way, there’s very rarely a reason to have an entirely abstract superclass.

2

In most OOP languages, an implementing class can derive only from one abstract class, but implement multiple interfaces.

4

In a language like C++ that allows multiple inheritance, and has no interfaces, abstract classes in which all methods are abstract can serve as interfaces. I haven’t worked with C++ that much, but I guess multiple inheritance can cause problems when there are methods with the same name in base classes.

In languages like PHP and C#, interfaces provide a means of achieving similar polymorphism, although I dislike calling it “inheritance” since there’s a conceptual difference between inheriting an abstract class and implementing an interface. Interfaces remove the conflict problem, because they themselves provide no implementation.

An interface serves as a contract to the outside world, while an abstract class can provide an implementation, although if used to “fake” an interface, it most likely won’t.

The main conceptual difference is that when a class inherits another class (abstract or not), there’s a relationship of “is”, so a Car is a Vehicle and Dog is an Animal. With an interface, it’s what the object does that matters. So both Car and Dog can Move() and the consumer knows it because they implement Movable, but a Car is definitely not a Dog, or an Animal. And the move implementation will be different (wheels vs legs) but the consuming code doesn’t and shouldn’t care. Interfaces are all about the consuming code, rather than the implementation.

The main point is if you have interfaces in you language of choice, use them for things that they’re there for. If not (like in C++), you can fake them by using pure abstract classes.

2

Abstract classes may feature abstract protected methods (in the languages I’m working with), in interfaces methods are usually always public. Whether this difference allows useful exploitations I don’t know.

Edit I first thought that a private abstract method is of no use, but now I recalled that it may be used to ensure that that method is never called. This way you can prevent that a copy constructor of an object is called.

3

Yes, they are different. Otherwise the language designers wouldn’t have provided both. I know of two languages that separate classes and interfaces: Java and C#, mutant clone of Java. The designers created interfaces to avoid supporting multiple class inheritance. Most other languages support multiple class inheritance and consequently don’t separate classes and interfaces.

I think the main difference is that : In an abstract class – even if all the methods are all abstract they can still provide data members (instance variables) and some code to the classes that implement it (in form of private methods or constructors), static blocks; doing part of the work for the sub classes and helping them with the implementation.

One positive side effect : code is in one place so one place to make corrections. sub classes can just call super class method and then do something else or nothing if super class actions are enough for their case. Super class wants each sub class to make this decision so has marked all its implementation as abstract (some could be empty too)

Not relevant to question: but having interfaces means one class can implement two different contracts. So that is an advantage of a interface over a abstract super class

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật