Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi,

I have no idea on what is difference creating instance and inheritance of a class in another class and how many ways can i access a class from one class to another class.
Posted

Creating an instance is the way that you can actually use the class: it is like buying a car.

Until you buy a car, you can't drive it when you want: with a class, until you create an instance you can't use it's properties and methods.

C#
MyClass mc = new MyClass();
Creates an instance of MyClass which you can now access via the variable mc - which is a reference to the actual instance. That sounds a little confusing (and without picture it is) but think of tit this way: mc is not the actual MyClass instance itself, it just tells the system where to find it - you can change MyClass without affecting the instance:
C#
MyClass mc;
mc exists, but it is null - it has no instance associated with it, and any attempt to use mc will cause an "Object reference not set to an instance of an object" error.
C#
mc = new MyClass();
Creates an instance of MyClass, and assigns it to mc - you can now access the properties of the class instance:
C#
mc.Text = "My first instance";
Console.WriteLine(ms.Text);
You can also create other variables, and other instances:
C#
MyClass mc2 = new MyClass();
mc2.Text = "My second instance";
Console.WriteLine(mc);
Console.WriteLine(mc2);
Will write
My first instance
My second instance
You can copymc
C#
MyClass mc3 = mc;
Console.WriteLine(mc);
Console.WriteLine(mc2);
Console.WriteLine(mc3);

Will write
My first instance
My second instance
My first instance
You can change mc
C#
mc = new MyClass();
mc.Text = "My third instance";
Console.WriteLine(mc);
Console.WriteLine(mc2);
Console.WriteLine(mc3);
Will write
My third instance
My second instance
My first instance
There is more too it, but that should give you enough for the moment!

Inheritance is different: it doesn't create anything you code can use without an instance of the new class being created - what it does do is allow the new class to have all the properties and methods of the inherited class without having to write them all again.
If you like, the make of a car (Ford, BMW, etc.) is an inherited class from Car, because the all have four wheels, an engine, a steering wheel, and so forth. The Ford class adds an oval badge, the BWM class adds a round badge and so forth. If you can Drive a Car, you can Drive a Ford, and a BMW, and all the other makes which are derived from Car.
 
Share this answer
 
Inheritance, together with encapsulation and polymorphism, is one of the three primary characteristics (or pillars) of object-oriented programming. Inheritance enables you to create new classes that reuse, extend, and modify the behaviour that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class.

A derived class can have only one direct base class. However, inheritance is transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA, ClassC inherits the members declared in ClassB and ClassA.

Classes can inherit from another class. This is accomplished by putting a colon after the class name when declaring the class, and naming the class to inherit from—the base class—after the colon.

Take a look at this.....

C#
/// <summary>The Class Car - A normal car.
    /// </summary>
    public class Car
    {
        /// <summary>Creates a new car object.
        /// </summary>
        public Car()
        { }

        public virtual string Plate
        { get; set; }

        public void Drive()
        { }

        public void Reverse()
        { }
    }

    /// <summary>A type of Advanced car.
    /// </summary>
    public class SuperCar : Car
    {
        public SuperCar()
        { }

        public void Fly()
        { }

        public void Swim()
        { }
    }

    /// <summary>BatMan's car.
    /// </summary>
    public class BatMobile : SuperCar
    {
        /// <summary>Creates a new instance of this class.
        /// </summary>
        public BatMobile()
        { }

        public void Weapons()
        { }

        public override string Plate
        {
            get
            {
                return base.Plate;
            }
            set
            {
                base.Plate = value;
            }
        }
    }


Notice, how the classes are inherited?

The First class car can Drive and can Reverse, it also has a virtual string which means that it can be modified in other derived classes.

The second class Supercar, can Fly and Swim but it can also Drive and Reverse because it has inherited it from it's base class Car

Now, the class BatMobile only has one method Weapons, but it can also Fly, Swim, Drive and Reverse because it has inherited it from it base class SuperCar which also inherited it from Car.

Other examples.

So to create an instance of the class BatMobile, this is like preparing to use the car BatMobile... In C#

C#
BatMobile BatCar = new BatMobile()


In VB

VB
Dim BatCar as new BatMobile()


Now that we have created the car, we can now use it to do whatever we want.

C#
BatCar.Drive();
BarCar.Fly();


Note that all classes inherit the Object class from System.Object

Other ways of instantiating...
 
Share this answer
 
v2
creating instance and inheritance of a class are different
creating instance means creating object of the class and by using that object access properties and methods of that class.
but by using inheritance you can create another class from that (base) class which contains properties of base class as well as new properties of that class.
 
Share this answer
 
 
Share this answer
 
both are different things
see relations & examples
Inheritance
Class  <->  class/Interface  
e.g. in simple language, child have some habits of parents. [it is called inheritance]

suppose,
a class named person have properties,
name, birthdate,address...
now, create a class employee [employee it self a person so, it can inherit class person]
employee class have property 
grade, basicSalary, JoiningDate... 
and it have inherited class person so, that properties are also included in employee class now,
employee class has properties 
name, birthdate,address...grade, basicSalary, JoiningDate...

Instance
Class  <->  Object
e.g. note book have date & page index on each page it is fix- but we will fill date & page index different for each page
so, this case,
notebook is class have property Date, PageIndex
every new page of notebook is instance that can hold different data then each other.

Happy Coding!
:)
 
Share this answer
 
Comments
Akinmade Bond 9-Oct-12 7:46am    
Nice concept.
Aarti Meswania 9-Oct-12 7:47am    
thank you!
:)
Inheritance is diffrent than to create an object & have own usage scenarios

Incase the class you focus, is based on some other class' properties & you want that class to be instantiated before you start to work within your class. in that case you need to inherit the base class.
otherwise
if you just want to
1- get some work done from any other utility class
2- want to pass control to any other class (temporarily or permanently)
3- to open it up as a separate process

in those cases you may go for instance creation.


in short, inheritance is used when base class need to participate in your class functionality at very initial level (while instantiation). whereas in you create an object of any other class, it depicts, the instantiated class is may necessary to complete your class' functionality but its prevention will not generate any error or flaw in your focused class.
 
Share this answer
 
When you Create one class such as "class Shape" and you add any members class to it and you make instances from this class. but when you inheritance from one Base Class any members class on the base class Copies into "your class" and you can access those members if you have used public or protected access modifier. 
 
Share this answer
 

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