Difference between Inheriting and creating object of a class?

Instantiating an object (class) is when you're using an instance of it.

Inheriting an object (class) is when you're building more functionality (or changing current functionality) of said class.

Even after you create a new class that inherits another one, you still have to instantiate it before you can actually use it.
 
To expand on this more, using some examples that may make more sense in a real world scenario.


You create a program, your program consists of a windows Form that displays on screen... and the form opens another form with some data shown in it.

Both of these windows are of type Form (they extend/inherit from System.Windows.Forms.Form). All the functionality that defines a 'form' is already written in the class. It can be rendered, it can be dragged, it has a grabby bar, etc etc etc. All that functionality needs code written... yet every time you create a new Form in your project (Form1, Form2, Form3) you don't have to write that code. That's because you inherited that code from System.Windows.Forms.Form

BUT just because you have defined your customer form, this doesn't mean it's an object. It's still just a class. The program creates an 'instance' of your form and displays it. Or in your own custom code you have say a button that when clicked you create an instance of another form or dialog, and show it.

Inheritance - you inherit the code of some super class
instance - a given object created from a class...



instances and inheritance are barely related in this sense. Of course both are important, but you don't need one to have the other... and they both accomplish two completely different things. It has nothing to do with properties or methods. Anyways, where do you think all those properties and methods already available on your classes come from???
 
Back
Top