C# Abstract class issue

Hi I wrote Abstarct class but i need to know this correct,i feel something is not write see below my code
Abstarct class
 abstract class Display
    {

       public abstract void DisplayDetails();
        

    }
}

Open in new window


Calling
 class Email:Display
    {

        private string strEmail;
        private string fromPassword = "123456";

        public string EmailResults
        {
            get { return this.strEmail; }
            set { this.strEmail = value; }
        }

        public override void DisplayDetails()
        {
            EmailResults = "Send email successfully!!!";
        }

Open in new window


Please let someone know where is wrong ,how to do implement with abstract class.thx
LVL 10
ukerandiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anarki_jimbelSenior DeveloperCommented:
Hi,

Technically it is correct.

The thing is that we need to understand what the "void DisplayDetails()" method is intended for.
Do we use it for "displaying" something? Or, e.g., to "display" something (print, for example")?
Or, may be, just set up some environment/results?

Personally I do not like the way method is used. If the consumer of the method is "outside" the class he won't see any "immediate" effect  - nothing would be displayed. An the consumer of this public method has no knowledge of the implementation. So, I'd either changed the name  of the method or, e.g., changed the method to accept a string argument (we may want to "display" different things), or may be something else.... - I have no idea about the intention...
Jacques Bourgeois (James Burger)PresidentCommented:
As far as the code is concerned, everything is OK.

The problem, as is often the case in object oriented programming, is not with the code. It is with the design.

The idea of an abstract class is to be able to have common methods and/or properties for a group of derived class, so that these can be all used in the same place through the base class (polymorphism).

But in your case, you implement the abstract method by setting a property. Other classes that derive from your abstract class do not have to implement that property. So what will they be doing with the method?

If they are doing something else, then there is no reason to have an abstract class or method. An abstract method is supposed to do the same thing in all the derived classes that needs to have different ways of doing it. Playing a video or playing a music file requires different code, but ends up doing the same thing: "play" the file.

If the role of your abtract method is to set a EmailResults property in all the methods that implement the abstract class, then you have no need for an abstract method. Simply have an abstract EmailResult property.

And all this is very confusing because the name of your method is a very bad one. Why a method called DisplayDetails, when it does not display anything? The name of a method should give a clear idea of what it does.

What is your purpose with that code? Maybe you are not using the appropriate technique for what you want to do.
it_saigeDeveloperCommented:
To give an example of what James is stating; consider the following:

Non-required abstraction - Virtual methods do not have to be implemented in the derived classes.
using System;

namespace EE_Q28689715
{
	class Program
	{
		static void Main(string[] args)
		{
			Bear bear = new Bear();
			Dog dog = new Dog();
			Fish fish = new Fish();

			Console.WriteLine(bear.MakeNoise());
			Console.WriteLine(dog.MakeNoise());
			Console.WriteLine(fish.MakeNoise());
			Console.ReadLine();
		}
	}

	class Animal
	{
		public string Name { get; set; }

		public virtual string MakeNoise() 
		{
			return string.Format("A {0} does not make any noise.", Name);
		}

		public Animal(string name)
		{
			Name = name;
		}
	}

	class Bear : Animal
	{
		public Bear() : base("Bear") { ;}

		public override string MakeNoise()
		{
			return string.Format("A {0} roars.", Name);
		}
	}

	class Dog : Animal
	{
		public Dog() : base("Dog") { ;}

		public override string MakeNoise()
		{
			return string.Format("A {0} barks.", Name);
		}
	}

	class Fish : Animal
	{
		public Fish() : base("Fish") { ;}
	}
}

Open in new window

Which produces the following output -Capture.JPG
Required abstraction - Abstract methods have to be implemented in the derived classes.  Also only abstract classes can contain abstract methods.
using System;

namespace EE_Q28689715
{
	class Program
	{
		static void Main(string[] args)
		{
			Bear bear = new Bear();
			Dog dog = new Dog();
			Fish fish = new Fish();

			Console.WriteLine(bear.MakeNoise());
			Console.WriteLine(dog.MakeNoise());
			Console.WriteLine(fish.MakeNoise());
			Console.ReadLine();
		}
	}

	abstract class Animal
	{
		public string Name { get; set; }

		public abstract string MakeNoise(); 

		public Animal(string name)
		{
			Name = name;
		}
	}

	class Bear : Animal
	{
		public Bear() : base("Bear") { ;}

		public override string MakeNoise()
		{
			return string.Format("A {0} roars.", Name);
		}
	}

	class Dog : Animal
	{
		public Dog() : base("Dog") { ;}

		public override string MakeNoise()
		{
			return string.Format("A {0} barks.", Name);
		}
	}

	class Fish : Animal
	{
		public Fish() : base("Fish") { ;}

		public override string MakeNoise()
		{
			return string.Format("A {0} does not make any noise.", Name);
		}
	}
}

Open in new window

Which produces the same output as above.

Mixed abstraction - You can mix both but remember abstract methods can only be declared in abstract classes.
using System;

namespace EE_Q28689715
{
	class Program
	{
		static void Main(string[] args)
		{
			Bear bear = new Bear();
			Dog dog = new Dog();
			Fish fish = new Fish();

			Console.WriteLine("Make primary noises");
			Console.WriteLine(bear.MakePrimaryNoise());
			Console.WriteLine(dog.MakePrimaryNoise());
			Console.WriteLine(fish.MakePrimaryNoise());

			Console.WriteLine();

			Console.WriteLine("Make secondary noises");
			Console.WriteLine(bear.MakeSecondaryNoise());
			Console.WriteLine(dog.MakeSecondaryNoise());
			Console.WriteLine(fish.MakeSecondaryNoise());
			Console.ReadLine();
		}
	}

	abstract class Animal
	{
		public string Name { get; set; }

		public virtual string MakeSecondaryNoise()
		{
			return string.Format("A {0} does not make a secondary noise.", Name);
		}

		public abstract string MakePrimaryNoise(); 

		public Animal(string name)
		{
			Name = name;
		}
	}

	class Bear : Animal
	{
		public Bear() : base("Bear") { ;}

		public override string MakePrimaryNoise()
		{
			return string.Format("A {0} roars.", Name);
		}

		public override string MakeSecondaryNoise()
		{
			return string.Format("A {0} growls.", Name);
		}
	}

	class Dog : Animal
	{
		public Dog() : base("Dog") { ;}

		public override string MakePrimaryNoise()
		{
			return string.Format("A {0} barks.", Name);
		}

		public override string MakeSecondaryNoise()
		{
			return string.Format("A {0} yelps.", Name);
		}
	}

	class Fish : Animal
	{
		public Fish() : base("Fish") { ;}

		public override string MakePrimaryNoise()
		{
			return string.Format("A {0} does not make a primary noise.", Name);
		}
	}
}

Open in new window

Which produces the following output -Capture.JPG-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ukerandiAuthor Commented:
great
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.