473,385 Members | 1,734 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,385 software developers and data experts.

Abstract class and Interfaces

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.
Mar 30 '07 #1
5 2301
Motoma
3,237 Expert 2GB
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.
Apr 2 '07 #2
JosAH
11,448 Expert 8TB
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
Apr 3 '07 #3
Ganon11
3,652 Expert 2GB
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.
Apr 3 '07 #4
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.
Aug 9 '07 #5
praveen2gupta
201 100+
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.
Aug 11 '07 #6

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

Similar topics

8
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...
6
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...
9
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...
5
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...
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
9
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
4
by: N.RATNAKAR | last post by:
hai, what is abstract class and abstract method
6
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...
5
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.