Click here to Skip to main content
14,922,492 members
Please Sign up or sign in to vote.
1.80/5 (5 votes)
See more:
What is Defference between Abstract class And Interface? with Real time Example in Hospital managment System.
Posted
Comments
[no name] 9-Oct-12 7:46am
   
What do an abstract class and an interface definition have to do with a hospital management system?

Rajesh Sanandiya wrote:
What is Defference between Abstract class And Interface?
This is a legitimate question, even if you could find yourself many answers just Googling for[^].


Rajesh Sanandiya wrote:
with Real time Example in Hospital managment System.
This definitely smells of homework.
Please read carefully one (or more) of the found explanations and then try yourself to formulate the example.
   
Comments
   
Sorry, but I really cannot agree that this is a "legitimate question". Formally, it's quite incorrect, bit informally -- I answered myself in detail, please see. I hope the incorrectness is explained, too. :-)
--SA
CPallini 9-Oct-12 13:29pm
   
I don't get you (e.g. why "what is the difference between {0} and {1}" is incorrect? BTW what do you mean with '{0}', '{1}'?).
   
I mean .NET formatting... :-); whatever you substitute for {0} and {1}. As I say, if such questions can be considered correct, tell us the difference between apple and Apple. (Other examples in my answer.)

The "difference" is really a figure of speech without exact meaning. Always. It can be applied in everyday speech, but not always, only when some subjects have a lot of in common. In formal systems like languages and APIs it's too risky even if there are a lot of things in common, like with interface and and abstract class (notably, when COM interfaces were introduced, they were modeled over the binary layout and properties of C++ classes without data with pure virtual functions only, but conceptually, .NET interfaces are not exactly the same things, because they are directly represented in IL -- do you see the delicacy of the matter? -- considering those .NET interfaces as a special kind of "pure abstract classes" would be too risky, even though technically there are similar).

I think I explained the matter in my previous answer in sufficient detail, but there could be more written on this topic, which is, as I say, is not completely trivial.
--SA
CPallini 9-Oct-12 13:59pm
   
Well, the I don't agree. Given all the possible subtleties, still the question is valid (moreover you should take into account the Java tag).
   
Well, it means we use different criteria for validity; not a big deal.
--SA
CPallini 9-Oct-12 15:34pm
   
:-)
What is an Interface?[^]


abstract classes
[^]

read that. twice, better three times.

You will figure that an abstract class is a parental one, such as "room" before knowing if it's a patients room or a doctor's office or even a waiting room.
An Interfaceis like a medic, he has certain skills that every medic should have, no matter if he's a nurse, doctor or paramedic.

Now it's up to you to write a code for that and to fill that with live.
Have fun and ask when you are not sure.
   
v2
Comments
   
Best answer so far, my 5. I also emphasize incorrectness of questions like "what's the difference between {0} and {1}?"...
--SA
   
So, after all I found 4 links to my more detailed answered to related questions, which are not completely trivial, as well as some advice on asking correct questions -- please see my answer.
--SA
Please see my past answer addressing related problems in detail:
When we use abstract and when we use interface...?[^],
Difference between abstract class and interface if they have same no of methods and var[^],
How to decide to choose Abstract class or an Interface[^],
Interfaces and Polymorphism[^].

You really need to understand that the questions in the form "what is the difference between {0} and {1}?" are incorrect. If you did not get it, please see my past answers:
what is the difference between the class and encapsulation in programming[^],
How to ask a good question?[^].

Please see the whole discussion on asking good questions; it can be quite useful:
How to ask a good question?[^].

By the way, did you follow these rules for an inquirer?
Code Project Questions and Answers FAQ[^]
How to Use Google and Other Tips for Finding Programming Help[^]
Some guidelines for posting questions in the forums[^])

—SA
   
Hi Rajesh. i explain to you very simple with my sample code i hope to help you.
interface is a Standardize way for your classes for now. see the example

:: When you Use of Interface ITV you are saying all of TV such as "Monitor","Home Theater","LCD","LED",and so on must have this features and method and in order to TV Can have more own features.
C#
//First Base Class
class Monitor : ITV
{
    // Fields
    public int Number_HDMI_Port { get; set; }
    public int Number_USB3_Port { get; set; }
    // Constructors
    // other Methods
    public void Power(bool on)
    {
        if (on) Console.WriteLine(" the TV is ON");
        else Console.WriteLine(" the TV is OFF");
    }
    // Indexers
}
// Second Base Class
class HomeTheater : ITV
{
    // Fields
    public int Number_HDMI_Port { get; set; }
    public int Number_USB3_Port { get; set; }
    // Constructors
    // other Methods
    public void Power(bool on)
    {
        if (on)
        {
            Console.WriteLine(" Shows the Menu for ON the Screen");
            Console.WriteLine(" The TV  is ON");
        }
        else
            Console.WriteLine(" the TV is OFF");
    }
    // Indexers
}
// Interface
interface ITV
{
    // Properties
    int Number_HDMI_Port { get; set; }
    int Number_USB3_Port { get; set; }
    // Methods
    void Power(bool on);
}

:: but when you can use the Abstract Class that You Have one or more Abstract Members Class. Such as a Method that Has no body and it's can not "static" or "virtual"
Example:
C#
// Base Class
abstract class TwoDShape
{
    // Fields
    private double width;
    public double Width
    {
        set { this.width = value < 0 ? -value : value; }
        get { return this.width; }
    }
    private double height;
    public double Height
    {
        set { this.height = value < 0 ? -value : value; }
        get { return this.height; }
    }
    // Constructors
    // other Methods
    public abstract double Area(); // abstract Method so You Must own Class change to Abstract Class
    // Indexres
}
class Triangle : TwoDShape
{
    // Methods
    public override double Area() // Override Area Method
    {
        return (Width * Height) / 2;
    }
}
class Rectangle : TwoDShape
{
    // methods
    public override double Area() // own implimentation
    {
        return Width * Height;
    }
}
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900