Member Avatar for majorawsome

Alright. So with c# we have SINGULAR class inheritance, correct? But we can have infinite Interfaces. I don't get this because when I inheret like so (I'm doing this right, Right?)

class Player : Zombie_Game.Variables
    {
        
    }

and in my Variables class I have this...

class Variables
    {
        int x = 4;
        int y = 9;
    }

If it does what I think it does (Lets me use everything in Variables in Player class)
why can't I call x and use it? Instead I have to create an instance of it (If thats the correct term for it) and put Variables.X to use x. Why does this happen? Can't I just use put x when I wan't to use x?

hello,
Please use "public" or "protected" for your "x" and "y" variables

protected int x = 4;

Cheers

Member Avatar for majorawsome

? I don't get this. I want to see if I can directly call x. Not with a class instance because I can do that without it have to be the base class so I'm trying to see what the difference is with a base class

Try this:

class Player : Variables
    {
             public int getX()
             {
                return this.x;
             }
    }

    class Variables
    {
        protected int x = 4;
        public int y = 9;
    }

You can not use x or y from your Variables class because it has private modificator.

Member Avatar for majorawsome

Lets restart. What is the differance from Inheriting and Initializing a class. Like

class Tomato : Seed

vs

Seed seed = new Seed();

You have created the instance of the Seed class. But this is not any Tomato class.

Member Avatar for majorawsome

But what if we put the instance of seed in the tomato class?

class Tomato : Seed
{
    Seed seed = new Seed();
}

Either way the new instance of seed will be needed so why even bother with inheriting the class?

OK

Tomato tomato = new Tomato()

You have got the instance of the Tomato class what inherits Seed class.

Please note, you are able to have access to all protected and public variables in Seed class.

Member Avatar for majorawsome

Doesn't Tomato inherit seed class? doesn't that make it open to all of its variables?

Member Avatar for majorawsome

Ok. They all have to be public or protected. but for inheriting seed class shouldn't you be able just to directly calls a variable instead of doing seed.variable

class Tomato : Seed
{
    Seed seed = new Seed();
}

In this example you have got another instance of the Seed class inside Tomato class. But this Seed class (new I mean) is not base class for Tomato.

Doesn't Tomato inherit seed class? doesn't that make it open to all of its variables?

Tomato tomato = new Tomato() Yes. In this case you have got access to all Seed variables, but private.

Member Avatar for majorawsome

Alright, then what does Inheritance only let me do? like so...

class Tomato : Seed

I think you are referring to the difference between encapsulation vs inheritence. They are well documented topics on the net so look up a few articles and see if that answers your question. If not post then please post back.

fella i think that we need instance of class because we can only access object properties not class properties itself.class is like a blueprint that has no real world existence .

Member Avatar for majorawsome

Alright, You were right about the inheritance vs encapsulation. Although I could not find any of these that didn't go into depth I will try to find some more so other people coming to look on this will have an answer. Can someone change this thread to "Enscapulation VS Inheritance"? Thanks.

Member Avatar for majorawsome

I have decided to provide an example of Encapsulation. (Please feel free to correct me as I am not in the IDE which I am used too)

using System;

Namespace Encapsulation
    class MainClass
         public void Main
             Variables V = new Variables();// This is creating a new instance of the Variables class to call the Feilds, Methods Ect.

             Console.WriteLine("Hello, Variable X in the class Variables equals{0}",V.X);
             Console.ReadLine();

WHILE IN THE Variables class..

using System;

Namespace Encapsulation
    public class Variables
    
    public int X = 10;

The output of this program should look a little like this...

Hello, Variable X in the class Variables equals 10

This doesn't seem to have anything to do with interfaces. If you don't want to have an instance of a class while still accessing its members you should consider making it a static class. (Assuming you are using a newer version of C# that supports automatic properties)

namespace myProgram
{
  internal static class variables
  {
    public int x { get; internal set; }
    public int y { get; internal set; }
  } 
  public static class mainProgram
  {
    public main()
    {
      int mX = variables.x
      variables.y = 25
    }
  }
}

Well,
with the above conversations observed,
you cannot create an instance of an object inside the derived class as a class parameter, however what you have created 'Seed seed = new Seed(); , 'seed' object cannot be used further inside that class.
In case u need to create an object, you can do so inside a method.
You will be able to access only public variables of a class, and not the private variables through the object.
with out creating the object, if u need to access the public variables of the base class, u can use the keyword 'base', and this too being inside the method of the inherited class.

with you being stubborn to mark ur variables as public, there will be no way u can use that variable during inheritance. Why do you think there is access modifiers available if do not want to use them, its for ur data protection.

hope this solves your doubt, if yes, mark the thread as solved.

Member Avatar for majorawsome

Well,
with the above conversations observed,
you cannot create an instance of an object inside the derived class as a class parameter, however what you have created 'Seed seed = new Seed(); , 'seed' object cannot be used further inside that class.
In case u need to create an object, you can do so inside a method.
You will be able to access only public variables of a class, and not the private variables through the object.
with out creating the object, if u need to access the public variables of the base class, u can use the keyword 'base', and this too being inside the method of the inherited class.

with you being stubborn to mark ur variables as public, there will be no way u can use that variable during inheritance. Why do you think there is access modifiers available if do not want to use them, its for ur data protection.

hope this solves your doubt, if yes, mark the thread as solved.

Um, My variables are public in the example I gave. Im also pretty sure that that was a correct use of encapsulation when not inheriting

Um, My variables are public in the example I gave. Im also pretty sure that that was a correct use of encapsulation when not inheriting

No they are not. If you provide no private/protected/public/internal keyword then the variables default to private.

Often an example can do more to explain than an explanation. I tried to comment the example to explain what is going on as much as possible.

public class Parent
    {
        // public properties
        public int X { get; set; }
        public int Y { get; set; }

        // protected properties
        protected int Z { get { return 75; } }

        // private properties
        private int W { get { return 0; } }
    }

    public class Child : Parent
    {
        // the child object has access to protected properties
        public int Z { get { return base.Z; } }

        // This causes an error because private
        // properties are not accessible outside
        // of the class in which they are created
        public int W { get { return base.W; } }
    }

An example of using these classes in a simple console app

// Create a new child object and set its X and Y values
            Child child = new Child();
            child.X = 25;
            child.Y = 50;

            Console.WriteLine("child.X: " + child.X);
            Console.WriteLine("child.Y: " + child.Y);

            // A parent object can be created directly 
            // from an instance of a Child object
            Parent parent = child;
            Console.WriteLine("parent.X: " + parent.X);
            Console.WriteLine("parent.Y: " + parent.Y);
            
            // This will cause an error since Z is protected
            // it is only accessible by the class and classes
            // that inherit from it
            Console.WriteLine("parent.Z: " + parent.Z);

            // This will cause an error because W is private
            // and only accessible within its own class
            // and is not even available directly to a class
            // that inherits it
            Console.WriteLine("parent.W: " + parent.W);

            // However to create an instance of a Child object
            // from a Parent object we have to cast it as
            // a Child object
            Child child2 = (Child)parent;
            Console.WriteLine("child2.X: " + child2.X);
            Console.WriteLine("child2.Y: " + child2.Y);
            Console.WriteLine("child2.Z: " + child2.Z);

Note: this will not compile as is since errors were left in intentionally for demonstration purposes

Learning how polymorphism and base class inheritance work would be more benefitial to you than posting here then telling people that they are wrong when they are trying to help you.

Member Avatar for majorawsome

So basicaly you can only call something from a class if it is public or protected. Is there anyway to create everything public or protected without using the public modifier? it seems a bit redundent writing them infront of all of the variables. Also, We can put other instances of classes together? I tried doing this

Variables v = new Variables();
Methods m = v;

And it wouldn't allow me to do that. It says...
Cannot implicitly convert type 'Test_Console.Variables' to 'Test_Console.Methods'
Can you help me with that? Thanks!

if Variables inherited from Methods then that would work. However if Methods inherits from Variables then you would need to cast v as a Methods type.

Methods m = (Methods)v;

If neither inherit from the other then this will not work.

Member Avatar for majorawsome

if Variables inherited from Methods then that would work. However if Methods inherits from Variables then you would need to cast v as a Methods type.

Methods m = (Methods)v;

If neither inherit from the other then this will not work.

THANK YOU! Haha. We never really solved the original question though. Well we might have somewhere in these posts. I guess Ill close this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.