How do I fix this code for inheritance in visual C#?

  • C#
  • Thread starter MathematicalPhysicist
  • Start date
  • Tags
    Visual
In summary, the code does not compile because you cannot have a class inside of a method. You should learn more about object-oriented programming so that you can correct this error.
  • #1
MathematicalPhysicist
Gold Member
4,699
371
I have the following code which doesn't get compiled:
Code:
using System;
using System.Collections;

namespace ConsoleApplication5
{
    static void Main(string[] args) {
        class Program {
        public class Animal
        {
            public virtual void Greet()
            {
                Console.WriteLine("Hello, I'm some sort of animal!");
            }
        }
        public class Dog : Animal
        {
            public override void Greet()
            {
                Console.WriteLine("Hello, I'm a dog!");
            }
        }
    }
}
}
I used the following webapge:
http://csharp.net-tutorials.com/classes/inheritance/

Can you help me correct this code?

Thanks in advance.
 
Technology news on Phys.org
  • #2
Do you have any idea yourself? Try look at the error message and then at your code.

If this doesn't help, try read up on if the Main method (or any method, for that matter) can be placed directly in a namespace.
 
  • Like
Likes Silicon Waffle
  • #3
I don't know exactly what is causing the errors, I tried fixing this code as I have been suggested by someone on another QA site:
Code:
using System;
using System.Collections;

namespace ConsoleApplication5
{
    class Program
    {
        public class Animal
        {
            public virtual void Greet()
            {
                Console.WriteLine("Hello, I'm some sort of animal!");
            }
        }
        public class Dog : Animal
        {
            public override void Greet()
            {
                Console.WriteLine("Hello, I'm a dog!");
            }
        }
    }
}

But it seems I need to add the static void Main line, I was told to put it in the end of the code, i.e after Public class Dog : Animal {}

But it seems I need to call up the Dog class, how to do this?

Thanks.
 
  • #4
MathematicalPhysicist said:
I have the following code which doesn't get compiled:
You cannot define a class inside of a method. You also need the static main method because it is the entry point of your program. It's probably a good idea to learn the basic concepts of object oriented programming.
 
  • #6
Ok, I fixed it, here's the fixed code:
Code:
using System;
using System.Collections;

namespace ConsoleApplication5
{
    class Program
    {
        public class Animal
        {
            public virtual void Greet()
            {
                Console.WriteLine("Hello, I'm some sort of animal!");
            }
        }
        public class Dog : Animal
        {
            public override void Greet()
            {
                Console.WriteLine("Hello, I'm a dog!");
            }
        }
        static void Main(string[] args)
        {
            Dog dog = new Dog();
            dog.Greet();
            Console.ReadKey();
        }

    }
   
}
 

1. What is "Inheritance" in Visual C#?

Inheritance in Visual C# is a concept that allows a class (called a "child" or "derived" class) to inherit properties and methods from another class (called a "parent" or "base" class). This allows for code reuse and helps to create a hierarchy of classes.

2. How is Inheritance implemented in Visual C#?

In Visual C#, inheritance is implemented using the "colon" symbol (:) after the child class name, followed by the name of the parent class. This creates a relationship between the two classes, and the child class can then access the properties and methods of the parent class.

3. What is the difference between "single" and "multiple" inheritance in Visual C#?

In single inheritance, a class can only inherit from one parent class. This is the default behavior in Visual C#. In multiple inheritance, a class can inherit from multiple parent classes. However, this is not supported in Visual C# and instead, it uses a concept called "interface" to achieve similar functionality.

4. Can a child class override a method from the parent class?

Yes, a child class can override a method from the parent class by using the "override" keyword before the method declaration. This allows the child class to have its own implementation of the method, instead of using the one inherited from the parent class.

5. How does inheritance relate to the "is-a" relationship in object-oriented programming?

Inheritance represents the "is-a" relationship in object-oriented programming. This means that a child class is a type of its parent class, and it inherits all the characteristics of the parent class. For example, a "dog" class can inherit from an "animal" class, as a dog is a type of animal.

Similar threads

  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
Back
Top