Please help me understand this difference...

thetexan

New member
Joined
Dec 3, 2014
Messages
3
Programming Experience
1-3
When you create a class you are essentially creating a prototype...a template...for future copies of the prototype which are called objects. Similar to the Oasis of the Seas cruise ship. The Oasis is the flagship and all other ships made like her are referred to as Oasis class ships....Ohio class submarines...etc. Each is a duplicate of the first including all of the attributes and methods. Each object has 'inherited' the details of the parent class. Am I right?

To create this class I do this...

public class Dog
{
attribute
attribute
method
}


to create a object of the class which carries with it all of the class 'stuff' I do this...

Dog Canine=new Dog();

which now allows me to refer to the 'stuff' of the class

canine.attribute
canine.method


So here is the question...

If I have created an instance of the class with my object what is the purpose of the inheritance operator...

public class Canine : Dog

What does this do that the first example doesnt?

tex
 
Those two code snippets are unrelated. As you say, a class is a template from which to create objects, i.e. instances of that class. When you inherit a class, you are creating another template that has all the members of the class it inherits plus any additional members you give it. For instance, the Control class defines a template for all controls and then each individual control class, e.g. TextBox, Label and ComboBox, inherit Control and add their own members that are specific to that type of control.
 
Those two code snippets are unrelated. As you say, a class is a template from which to create objects, i.e. instances of that class. When you inherit a class, you are creating another template that has all the members of the class it inherits plus any additional members you give it. For instance, the Control class defines a template for all controls and then each individual control class, e.g. TextBox, Label and ComboBox, inherit Control and add their own members that are specific to that type of control.

So if I understand you, in the first case I created a object from a class (a duplicate of the template) which has (or inherited, by the dictionary sense) everything from the parent class since it is a cookie cutter duplicate.

In the second case, I would be creating another class (template) which can inherit (if instructed to do so) all parts of the copied class and, in addition, I can add to those attributes or methods.

So thru inheritance, I can shortcut my way to a new class based on the old class plus spiff it up to my specs. Any object made from the new class will be a copy of that....am I right?

tex
 
In the second case, I would be creating another class (template) which can inherit (if instructed to do so) all parts of the copied class and, in addition, I can add to those attributes or methods.
To my knowledge it's not 'can' but 'will'

For me, an easier example would be cars. All cars have a couple of things in common; those fields are declared in the car class. The classes that are derived from the car class inherit those fields. Each derived class has its own specific fields declared.
    class Car
    {
        public string make;
        public string model;
        public int enginecapacity;
        public int numcylinders;
        public int numgears;
        public int numseats;
public int numdoors;
    }

    class Fourbyfour : Car
    {
        public bool haslowrange;
        public bool hascenterdiff;
    }

    class Truck : Car
    {
        public int numaxles;
        public int numdrivenaxles;
        public bool haslowrange;
        public int loadcapacity;
    }

As the car class covers everything for a passenger car, you can use it directly.
// passenger car
Car pc = new Car();
pc.make = "bmw";
pc.model = "320i";
pc.numcylinders = 4;
// etc

For a fourbyfour
// a fourbyfour
Fourbyfour ff = new Fourbyfour();
ff.make="jeep";
// etc
//
// specific to 4x4
ff.haslowrange = true;
ff.hascenterdiff = true;

And for a truck
Truck tr = new Truck();
tr.make = "volvo";
// etc
//
// specific to truck
tr.haslowrange = true;
tr.numaxles = 3;
tr.numdrivenaxles = 2;
tr.loadcapacity = 10000;

Another example that might be easier to understand for computer people
    abstract class ComputerBoard
    {
        public int controladdress;
        public int interruptnumber;
        public string interfacetype; //e.g. isa, eisa, pci
    }

    class VideoBoard : ComputerBoard
    {
        public string videochip;
        public string memory;
        public int numoutputs;
    }

    class UsbBoard : ComputerBoard
    {
        public int numports;
    }

    class FranegrabberBoard : ComputerBoard
    {
        public int memory;
        public int numinputs;
    }

All computer boards have some things in common; those are declared as fields in the computerboard class. Next there are (in the example) three types of computer boards (video board, usb board and frame grabber board). They all inherit the three fields of the computerboard class; for each of them some additional fields are declared to the type of board.

In this example, the computerboard class can not be instantiated (due to the abstract keyword). Reason is that it does not make sense (to me) to have a computerboard that does not provide a functionality.
 
So if I understand you, in the first case I created a object from a class (a duplicate of the template) which has (or inherited, by the dictionary sense) everything from the parent class since it is a cookie cutter duplicate.
You are confusing yourself by using "inherited" there. If you were a designer for a car company and you drew a technical design for a new car, would you say that each car that rolled of the assembly line inherited its attributes from your design? I suppose you could but I don't think many people would. Each car that is built is the embodiment of the design. It manifests the attributes of the design rather than inheriting them.
In the second case, I would be creating another class (template) which can inherit (if instructed to do so) all parts of the copied class and, in addition, I can add to those attributes or methods.
To stick with the car analogy, you might create a new design for a car based on the old one, so you start with all the features that the original design had but you add a few new features. The new design has inherited all the attributes of the old design and then added some of its own, possibly changing the details of some existing features, e.g. leather upholstery or a more powerful engine.
So thru inheritance, I can shortcut my way to a new class based on the old class plus spiff it up to my specs. Any object made from the new class will be a copy of that....am I right?
That's pretty much true. A base class has some specific functionality and a derived class implicitly has all that same functionality plus it can add some new functionality of its own and/or modify some of the existing functionality.
 
Back
Top Bottom