Jump to content

Definition of Inheritance - Object-Oriented Programming

- - - - -

  • Please log in to reply
5 replies to this topic

#1
gacanepa

gacanepa

    Newbie

  • Members
  • PipPip
  • 19 posts
Hi all!
I am a C# newbie. That is the first programming language I've seriously put time and effort into self-studying.
I am relatively confortable with building small applications but when it comes to concepts, it's a different story.
Now let me get to my question: I know that for a programming language to be considered Object-Oriented, it needs to deal effectively with 4 major features:
  • Encapsulation
  • Polymorphism
  • Inheritance
  • (am I missing any other?)
When I build a class and add it to an existing project, I can create as many objects from it using the "new" statement (that is what is called an instance of that class, right?) and all methods included in that class will be available for them as well. Is that what INHERITANCE (as a OOP feature) refers to?
Also, what are some examples (easy to understand, remember I'm a newbie :rolleyes:) of the other OOP features?
(Another question: how is my english? please feel free to correct any spelling mistakes and to point out any needed grammar improvement in order to make myself better understood in the future).

#2
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
inheritance is here for not coding to much...if you need in some class the same methods or other stuffs from it to another class you will use inheritance instead writting same code again

and for class instancig the syntax is next:


class Car{

...

}

class MyProgram{

Car car = new Car();

//and if you want to use some method from it you can do like this:

car.SomeMethod();

}

but if you want to avoid class instancing you will need to declare your class as static like this:


static class Car{

...

}

class MyProgram{

Car.SomeMethod();

}


you will need to search on google what is the difference between static class and non static class because i forgot it...

btw your english is not bad

#3
Revolt

Revolt

    Programmer

  • Members
  • PipPipPip
  • 99 posts
Your English is very good so don't worry about it ;)

Lets now try to show you the meaning of each of the features you enumrated.

Picture this: you want to model various races of dogs. For our example lets consider a pitbull and a pug.

abstract class Dog {

    virtual void move() {

          Console.WriteLine("Run like mad");

    }


    virtual void talk() {

          Console.WriteLine("Bark!");

    }

    

    abstract void whoAmI();

}


class Pug : Dog {

    override void move() {

         Console.WriteLine("Take little steps since I'm little.");

    }

    

    void whoAmI() {

         Console.WriteLine("I am a pug!");

    }

}


class Bulldog : Dog {    

    void whoAmI() {

         Console.WriteLine("I am a bulldog!");

    }

}

Pug and bulldog inherit from dog so they also inherit dog's methods: move, talk and whoAmI. This process is called inheritance.

They can choose to override those methods (assuming they were defined as virtual in the parent class) like Pug did with the move method. This process is called overriding (makes sense right?).

This structure allows the following interaction:

Pug p = new Pug();

Bulldog b = new Bulldog();

p.talk() -> "Bark!"

b.talk() -> "Bark!"

p.whoAmI() -> "I am a pug"

b.whoAmI() -> "I am a bulldog"

p.move() -> "Take little steps..."

b.move() -> "Run like mad"

Notice that we never got to define the talk methods on the Pug and Bulldog classes but since their parent class (Dog) defined it, those classes also have it. That's one of the powers of inheritance.

Another possible interaction:

Dog dog1 = new Pug();

Dog dog2 = new Bulldog();

dog1.whoAmI() -> "I am a pug"

dog2.whoAmI() -> "I am a bulldog"

Notice that we are now handling a pug and a bulldog by referring to the parent class. This is called polymorphism (one of the many types that exist). A class can be referred to as either itself or one of the parent classes.

As for encapsulation, it has to do with hiding the details of the implementation and the bundling of common data. Picture a Collection class. A collection can be implemented in a number of ways. The two most common are an array or a list. However, in terms of interaction, it doesn't really matter to the user what kind of implementation it uses. This could be accomplished with the following interface:

class Collection {

     public int getItem(int position);

     public void addItem(int position, int value);


     private int[] collectionArray;

     private List<int> collectionList;

}

By using the getItem and addItem functions the particular implementation of the collection doesn't matter. Notice that because collectionArray/List is private, the user is forced to use your wrapper functions!

I think that covers the basics. For more info check out wikipedia! It has an extensive explanation of each of the features.

#4
gacanepa

gacanepa

    Newbie

  • Members
  • PipPip
  • 19 posts

Tonchi said:

inheritance is here for not coding to much...if you need in some class the same methods or other stuffs from it to another class you will use inheritance instead writting same code again

and for class instancig the syntax is next:


class Car{

...

}

class MyProgram{

Car car = new Car();

//and if you want to use some method from it you can do like this:

car.SomeMethod();

}

but if you want to avoid class instancing you will need to declare your class as static like this:


static class Car{

...

}

class MyProgram{

Car.SomeMethod();

}


you will need to search on google what is the difference between static class and non static class because i forgot it...

btw your english is not bad

Tonchi, thank you so very much for taking the time to answer my question! It was very helpful!
PS: I have a book that says that when you define a variable as "local" (I think that's what you mean by "non-static") you can use it ONLY inside the code block where it is created. Otherwise, if it's static, it keeps its value when the block ends and you can refer to it afterwards. (Since the book is in Spanish, I hope I made a good translation! :w00t:)

#5
gacanepa

gacanepa

    Newbie

  • Members
  • PipPip
  • 19 posts

Revolt said:

Your English is very good so don't worry about it ;)

Lets now try to show you the meaning of each of the features you enumrated.

Picture this: you want to model various races of dogs. For our example lets consider a pitbull and a pug.

abstract class Dog {

    virtual void move() {

          Console.WriteLine("Run like mad");

    }


    virtual void talk() {

          Console.WriteLine("Bark!");

    }

    

    abstract void whoAmI();

}


class Pug : Dog {

    override void move() {

         Console.WriteLine("Take little steps since I'm little.");

    }

    

    void whoAmI() {

         Console.WriteLine("I am a pug!");

    }

}


class Bulldog : Dog {    

    void whoAmI() {

         Console.WriteLine("I am a bulldog!");

    }

}

Pug and bulldog inherit from dog so they also inherit dog's methods: move, talk and whoAmI. This process is called inheritance.

They can choose to override those methods (assuming they were defined as virtual in the parent class) like Pug did with the move method. This process is called overriding (makes sense right?).

This structure allows the following interaction:

Pug p = new Pug();

Bulldog b = new Bulldog();

p.talk() -> "Bark!"

b.talk() -> "Bark!"

p.whoAmI() -> "I am a pug"

b.whoAmI() -> "I am a bulldog"

p.move() -> "Take little steps..."

b.move() -> "Run like mad"

Notice that we never got to define the talk methods on the Pug and Bulldog classes but since their parent class (Dog) defined it, those classes also have it. That's one of the powers of inheritance.

Another possible interaction:

Dog dog1 = new Pug();

Dog dog2 = new Bulldog();

dog1.whoAmI() -> "I am a pug"

dog2.whoAmI() -> "I am a bulldog"

Notice that we are now handling a pug and a bulldog by referring to the parent class. This is called polymorphism (one of the many types that exist). A class can be referred to as either itself or one of the parent classes.

As for encapsulation, it has to do with hiding the details of the implementation and the bundling of common data. Picture a Collection class. A collection can be implemented in a number of ways. The two most common are an array or a list. However, in terms of interaction, it doesn't really matter to the user what kind of implementation it uses. This could be accomplished with the following interface:

class Collection {

     public int getItem(int position);

     public void addItem(int position, int value);


     private int[] collectionArray;

     private List<int> collectionList;

}

By using the getItem and addItem functions the particular implementation of the collection doesn't matter. Notice that because collectionArray/List is private, the user is forced to use your wrapper functions!

I think that covers the basics. For more info check out wikipedia! It has an extensive explanation of each of the features.

Your response was VERY helpful! And the examples you shared actually helped me understand a lot more about the basics of OOP. And what is even better, you used the console in your examples, which will help me learn more about it (since I've only used windows applications most of the time). Have a great day! :thumbup1:

#6
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts

gacanepa said:

Tonchi, thank you so very much for taking the time to answer my question! It was very helpful!
PS: I have a book that says that when you define a variable as "local" (I think that's what you mean by "non-static") you can use it ONLY inside the code block where it is created. Otherwise, if it's static, it keeps its value when the block ends and you can refer to it afterwards. (Since the book is in Spanish, I hope I made a good translation! :w00t:)

static keyword vs local has many different usages. However, your above interpretation of local is correct i.e. it can be used only inside the code block it is created. But this is only about variables in some piece of code.

static variables that are members of a class have a different notion. There exists a single copy of such a variable even if there are any number of objects created. For e.g. you created a class test and then created objects 1-10 of class test. Obviously there will be 10 different object containing all elements of class test EXCEPT static ones.

There would only be a single copy of static variable and that would exist even when no object of class has been created.

So when you create a static class, you cannot create it's objects, all of the elements of this class should be statics and you can use that class just like one permanent object of the same class.
Today is the first day of the rest of my life




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users