error CS0115: 'Pig.animalSound()': no suitable method found to override - c#

I'm implementing abstraction in c# and produces the error above
My code is:
using System;
namespace abstraction
{
abstract class Animal
{
public abstract animalSound();
public void sleep()
{
Console.WriteLine("ZZZZ");
}
}
class Pig : Animal
{
public override void animalSound()
{
Console.WriteLine("The pig says wee! wee!");
}
}
class Program
{
static void Main(string[] args)
{
Pig myPig = new Pig();
myPig.animalSound();
myPig.sleep();
}
}
}
I'll be glad to get some help.
I' expecting the output of:
The pig says: wee wee
Zzz

Related

c# Factory Method some Methods are not accessible

first, sorry for my bad English. I'm learning some design patterns. I try to implement a factory in an example-code. This is the Code.
Program.cs:
namespace Factorymethod_self
{
internal class Program
{
static void Main(string[] args)
{
Animal myDog = new Dogs().SummonAnimal();
myDog.Name = "Rex";
myDog.Run(); // Isn't working
myDog.Bark(); // Isn't working
myDog.Pet(); // Isn't working
}
}
}
Animal.cs:
namespace Factorymethod_self
{
public class Animal
{
private string name;
public string Name
{
get => name;
set => name = value;
}
}
}
Mammal.cs:
using System;
namespace Factorymethod_self
{
public class Mammal : Animal
{
public void Run() => Console.WriteLine(Name + " is running");
}
}
Bird.cs:
using System;
namespace Factorymethod_self
{
public class Bird : Animal
{
public void Fly() => Console.WriteLine(Name + " is flying");
}
}
GeneralFactory.cs:
using System;
namespace Factorymethod_self
{
public abstract class GeneralFactory
{
public abstract Animal SummonAnimal();
public void Pet()
{
Console.WriteLine("You petted " + SummonAnimal().Name);
}
}
}
Dogs.cs:
using System;
namespace Factorymethod_self
{
internal class Dogs : GeneralFactory
{
public void Bark() => Console.WriteLine("The Dog is barking");
public override Animal SummonAnimal()
{
Animal animal = new Mammal();
return animal;
}
}
}
I think, the method Bark() isn‘t accessible, because Animal myDog = new Dogs().SummonAnimal(); doesn’t impellent the class Dogs() but Mammal(). But I Doesn’t understand, why the methods Run() and Pet() aren’t accessible.
If I use GeneralFactory myDog = new Dogs();, I can access the Method Pet() but I don’t think, this is how the Factory should work.
I’ve added a map for the classes
So, there are my questions.
What am I doing wrong here?
How can I try to solve it?
Is this even a right factory?
Thank you very much.

A get or set accessor required with inheritance

I want to see is if the method bark will be overridden.
For some reason i am getting the error a get or set accessor required and another error when i try to call the method bark from main with a.bark.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace animals
{
class Animal
{
public void bark {
Console.WriteLine("woohhoo");
}
}
class dog : Animal
{
public void bark
{
Console.WriteLine("woof");
}
}
class Program
{
static void Main(string[] args)
{
Animal a = new Animal();
a.bark;
dog d = new dog();
d.bark;
}
}
}
You're missing the parenthesis on your method declarations:
Here: have a look at this fiddle
You should declare your method with () in C# and also call them using the parenthesis after the method name: a.bark();
class Animal
{
public void bark() {
Console.WriteLine("woohhoo");
}
}
class dog : Animal
{
public void bark()
{
Console.WriteLine("woof");
}
}
Call it like this:
public static void Main()
{
Animal a = new Animal();
a.bark();
dog d = new dog();
d.bark();
}
Also, no, the method will no override because in C#, if you want a derived type to override a method from the base class you must mark that method with virtual in the base and override it on the derived class.
Consider refactor your code like in this fiddle. Note that now you can create dog in an Animal variable and method is called correctly.
Animal d = new dog();
d.bark();
to override you need a virtual function. You also need brackets for functions. If you don't have brackets it thinks it's a property
which has the basic form :-
public void talk
{
get { return somevariable; }
set { somevariable = value}
}
which is why it's telling you it wants a get or set
Also calling it 'bark' is weird on Animal. So changing it a bit, what you want is something like :-
class Animal
{
public virtual void talk() {
Console.WriteLine("woohoo");
}
}
class dog : Animal
{
public override void talk()
{
Console.WriteLine("woof");
}
}
class Program
{
static void Main(string[] args)
{
Animal a = new Animal();
a.talk();
dog d = new dog();
d.talk();
// this part is key, when you have a dog as an animal, when you
// call talk it will use the overriden method. If you don't have
// virtual and override, then this would go "woohoo"
Animal da = d;
da.talk();
}
}
As mentioned in the comments, you need to have parenthesis after bark.
C# gives you a couple options in terms of overriding. Given the following main method.
static void Main(string[] args)
{
Animal a = new Animal();
a.bark();
dog d = new dog();
d.bark();
}
If you consider the following classes (Version A), this technique is called hiding. The dog's bark method hides Animal's. Notice the new keyword in front of the dog's bark method.
class Animal {
public void bark (){
Console.WriteLine("woohhoo");
}
}
class dog : Animal {
public new void bark() {
Console.WriteLine("woof");
}
}
output
woohhoo
woof
Now consider the following classes (Version B) where dog's bark method overrides animal's.
class Animal {
public virtual void bark (){
Console.WriteLine("woohhoo");
}
}
class dog : Animal {
public override void bark() {
Console.WriteLine("woof");
}
}
The output is the same. So what's the difference? Change the main method as follows and run the code with Version A and Version B.
static void Main(string[] args)
{
Animal a = new Animal();
a.bark();
Animal d = new dog();
d.bark();
}
Version A output
woohhoo
woohhoo
Version B output
woohhoo
woof

why can't a compiler resolve method overriding?

In the following C# snippet
public class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("Animal sound");
}
}
public class Dog:Animal
{
public override void MakeSound()
{
Console.WriteLine("Dog sound");
}
}
class Program
{
static void Main(string[] args)
{
Animal an = new Dog();
an.MakeSound();
Console.ReadLine();
}
}
the method to be called is determined at runtime. Why exactly can't the compiler figure out, which method to call?
Why doesn't the compiler see that an refers to a Dog object and then choose the method from that class?
And how does the runtime determine which method to be called?
This sounds an awful lot like an exam/homework question. But let me answer your question with another question.
Consider the following code:
static void Main(string[] args)
{
var random = new Random();
Animal an = null;
if (random.NextDouble() < 0.5) {
an = new Dog();
} else {
an = new Cat();
}
an.MakeSound();
Console.ReadLine();
}
How is the compiler supposed to know at compile time which method to call? It can not, only during runtime is the concrete type known.
You told the compilier that the variable is of type Animal. It looks only at declarations, while you expect it to execute your code to figure out what you mean. That's not how it works.
Consider following code:
class Program
{
static void Main(string[] args)
{
Animal dog = new Dog();
MakeSoundAbstract(dog);
Animal an = new Animal();
MakeSoundAbstract(an);
Console.ReadLine();
}
static void MakeSoundAbstract(Animal animal)
{
animal.MakeSound();
}
}
If compiler will determine virtual calls during compilation not during runtime then MakeSoundAbstract method will always execute MakeSound method of class Animal so we are loosing here the power of abstraction.
Consider this change to your code:
public class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("Animal sound");
}
}
public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Woof!");
}
}
public class Cat : Animal
{
public override void MakeSound()
{
Console.WriteLine("Purrrrrrrrrrrrrr");
}
}
class Program
{
static void Main(string[] args)
{
Animal an = GetAnimal(DateTime.Now);
an.MakeSound();
Console.ReadLine();
}
private Animal GetAnimal(DateTime dateTime)
{
if (dateTime.DayOfWeek == DayOfWeek.Monday)
{
return new Dog();
}
else
{
return new Cat();
}
}
}
Now it is impossible to know what type of animal to create at compile time, as it depends on the day of the week when the code is actually run. On Mondays you will get a Dog, but at any other time you will get a Cat. This is a defining benefit of polymorphism--types are not baked in by the compiler, but are derived on the fly when the code is executing. Polymorphism allows you work with these derived types, even though you do not know exactly what they will be when you write your code (but you do know they are all types of animals).

Error: Expected class, delegate, enm, interface, or struct in C#

Error in line : public virtual void BuyFavoriteStuff()
Error : Expected class, delegate, enm, interface, or struct
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LearnAbstractClass
{
class Program
{
static void Main(string[] args)
{
}
}
class Shopper
{
private int TotalSpent=0, CreditLimit=10;
public void ShopTillYouDrop()
{
while (TotalSpent < CreditLimit)
BuyFavoriteStuff();
}
}
public virtual void BuyFavoriteStuff()
{
// No implementation here - we don’t know
// what our student likes to buy!
}
class ArtStudent : Shopper
{
public override void BuyFavoriteStuff()
{
BuyArtSupplies();
BuyBlackTurtlenecks();
BuyDepressingMusic();
}
private void BuyBlackTurtlenecks()
{}
private void BuyDepressingMusic()
{}
private void BuyArtSupplies()
{}
}
class EngineeringStudent : Shopper
{
public override void BuyFavoriteStuff()
{
BuyPencils();
BuyGraphingCalculator();
BuyPocketProtector();
}
private void BuyPencils()
{}
private void BuyGraphingCalculator()
{}
private void BuyPocketProtector()
{}
}
}
What is the wrong in implementation above?
your method:
public virtual void BuyFavoriteStuff()
{
// No implementation here - we don’t know
// what our student likes to buy!
}
it outside of the Shopper class
You have method that is outside of any class:
public virtual void BuyFavoriteStuff()
{
// No implementation here - we don’t know
// what our student likes to buy!
}
just move it to Shopper class.
The method BuyFavoriteStuff is not inside class
public virtual void BuyFavoriteStuff()
{
// No implementation here - we don’t know
// what our student likes to buy!
}
You should declare your method inside class.

new Keyword confusion

All -
Have read various articles on the new keyword and when it should be used:
MSDN - http://msdn.microsoft.com/en-us/library/6fawty39(v=vs.80).aspx
StackOverflow - benefit of using new keyword in derived class member having same name with base class member
Here is a sample code I have written to practice this concept
static void Main(string[] args)
{
Animal a2 = new Dog();
a2.Talk();
a2.Sing();
a2.Greet();
a2.Dance();
Console.ReadLine();
}
class Animal
{
public Animal()
{
Console.WriteLine("Animal constructor");
}
public void Talk()
{
Console.WriteLine("Animal is Talking");
}
public virtual void Sing()
{
Console.WriteLine("Animal is Singing");
}
public void Greet()
{
Console.WriteLine("Animal is Greeting");
}
public virtual void Dance()
{
Console.WriteLine("Animal is Dancing");
}
}
//Derived Class Dog from Animal
class Dog : Animal
{
public Dog()
{
Console.WriteLine("Dog Constructor");
}
public new void Talk()
{
Console.WriteLine("Dog is Talking");
}
public override void Sing()
{
//base.Sing();
Console.WriteLine("Dog is Singing");
}
public new void Dance()
{
Console.WriteLine("Dog is Dancing");
}
}
Any my output is coming as follows:
Whats confusing me is that by using the new keyword in the derieved class is actually showing an output of the base class. Isnt that wrong - isnt new keyword supposed to hide the base class membership so the result for Animal is Talking and Animal is Dancing should not be printed.
Thanks
The "new" means the method is "new" and not an "override". Therefore, if you call a method by that name from the base, it hasn't been overriden so the derived won't be called.
This was confusing to me as well but to put it simply:
The new keyword hides the child's member from the parent.

Categories

Resources