Hi ,
I am new to the field of IT .I actually want to know according to what a designer selects it is good to use Abstract class or Interfaces in apartcular solution.
It would be more helpful if you describe it with some code examples?
Thanks in advance,
Ajai.
5 2305 Motoma 3,237
Recognized Expert Specialist
Hi ,
I am new to the field of IT .I actually want to know according to what a designer selects it is good to use Abstract class or Interfaces in apartcular solution.
It would be more helpful if you describe it with some code examples?
Thanks in advance,
Ajai.
The way I usually decide when to use inheritance is the is/has test. I say, is does my new class have this other class or is it of this other class.
An example:
I have classes Monster, and Hammer, and I want to create a class of type Troll.
I say to myself "Self, is a Troll a Monster, or does it have a Monster?" To which I answer "It is a Monster." I then say "Now Motoma, is a Troll a Hammer, or does it have a Hammer?" I then write my Troll class to be a subclass of Monster, and have a Hammer property.
JosAH 11,448
Recognized Expert MVP
The distinction of an abstract class and an interface differs per programming
language; e.g. C++ doesn't even have a keyword for an interface. Using C++
you simply build a class with all pure virtual functions.
Java does make the distinction and so it has an 'interface' keyword and its
associated semantics.
There's a little mantra in the OO world that says: "code to the interface, not the
implementation" . If you follow that little mantra at least one part of your design
will probably be fine. The next pitfall is the abuse of inheritance of both type
(interface) and implementation (classes).
Before you want to exend a type/class ask yourself the question if the inheritance
can stand the Liskov Substitution Principle (LSP) and even then think again
and consider composition over extension.
kind regards,
Jos
Ganon11 3,652
Recognized Expert Specialist
In the specific example of having to choose between an interface and an abstract class, it can get kind of tricky. I usually rely on the definitions of the two for what I'm doing.
An interface cannot define any of the methods it includes. Any class using the interface defines these methods - the interface itself has no idea of how its methods are implemented. Consider the Bird interface. Now, every Bird can fly, but the interface shouldn't know exactly how each bird flies. A hummingbird flies differently than a Robin, which flies differently than a Sparrow, and a Penguin doesn't even fly! Bird best fits as an interface.
In an abstract class, you can provide definitions for methods that all subclasses will share - you can choose as little as 1 method to be abstract. Consider the abstract class Employee. Now, every Employee has a first name, a last name, and an ID number. These are common to all Employees, so you can write the code to get/set these elements in the Employee class. But every Employee also gets a salary - however, an Hourly worker gets paid differently than a Contracted Worker. In this case, the getSalary() method would be abstract in the Employee class.
To summarize, use an interface when you don't know exactly how any implementing class will work, and use an abstract class when any subclasses will have some behavior in common.
I have seen the abuse of interfaces, as noted above to achieve multiple inheritance. This is irritating. An interface is to specify behavior. Then we dont need to use it to define class models, or data models.
Interfaces were introduced in CORBA/DCOM/COM era to facilitate distributed components specification only in terms of behavior. At that level, you are mainly focused in a component's behavior. But that does not mean that, every class you create will have to have an interface. In other words, you dont create interfaces while defining a good data model.
If you allow a component's organization in terms of entity classes, their associations, and relations, and a control class to manage these entities, collections, and relations, then specify an interface for the control class, and not for others.
No doubt you see, interfaces in libraries. Such interfaces could be for collections, etc. That is fine for those defining libraries. But designers have to think of using interfaces only for control classes. Otherwise, you get into doubling of classes. Also its a burden for the one who maintains code.
Hi ,
I am new to the field of IT .I actually want to know according to what a designer selects it is good to use Abstract class or Interfaces in apartcular solution.
It would be more helpful if you describe it with some code examples?
Thanks in advance,
Ajai.
In Java Both are ahving different purpose. It seems that there is some similarity that method are abstract in the both. Interface is used to implement the multiple inhertance. Since java does not supports multiple inheritance so this is an alternate for it. abstract is used when classes are at initial stage and we wants to bind the developers with certain conditions.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Vishal Gandhi |
last post by:
Hi ,
Please help me by advising an real life scenario where Abstract Classes
should be used over Interfaces or vice versa . Whats the basic difference
between Abstract Class and interface other then instantiation ?
Many Thanks
Vishal Gandhi
|
by: Dan Sikorsky |
last post by:
If we were to define all abstract methods in an abstract class, thereby
making that class non-abstract, and then override the heretofore 'abstract'
methods in a derived class, wouldn't that remove the need to have abstract
class types in C#?
Derived classes from abstract base classes must overrided the abstract
method defininition anyway,...
|
by: phl |
last post by:
hi,
I am kind of confused aobut interfaces and abstract classes.
In short as I understand it, an interface is like a contract between
the class and the interface, so that certain funtions must be
implemented. So if you have a class which inherits base class that
inherts an interface, then your classes will have a standard. I suppose
you...
|
by: Michael McCarthy |
last post by:
I want to develop plugin support for a system.montitor module I am
working on. A lot of the modules will do mostly interop stuff for an
older system, but I want to use it myself as well to monitor event logs
and other things as I think of them...
I've seen two ways of doing this, the abstract class factory, or the
interface... the module...
|
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 circumstacnes does one implement an
interface?
TIA,
--
Joe
VBA Automation/VB/C++/Web and DB development
| |
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
|
by: Sean Kirkpatrick |
last post by:
To my eye, there doesn't seem to be a whole lot of difference between
the two of them from a functional point of view. Can someone give me a
good explanation of why one vs the other?
Sean
|
by: N.RATNAKAR |
last post by:
hai,
what is abstract class and abstract method
|
by: Miguel Guedes |
last post by:
Hello,
I recently read an interview with Bjarne Stroustrup in which he says
that pure abstract classes should *not* contain any data. However, I
have found that at times situations are when it would be useful to have
/some/ data defined in such an abstract class for reasons of forming a
common block of data existing in or used by all...
|
by: =?Utf-8?B?UmljaA==?= |
last post by:
Greetings,
I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I
just want to ask about the relationship between Abstract Classes and
Interfaces.
My first question is if C# even has Iinterfaces. I took some Java
programming classes and Interfaces are a main staple of Java. And in VB.Net
I use interfaces for setting...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |