Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Static Class Attributes and Inheritance

Avatar for Ram Sapkota Ram Sapkota
October 28, 2018
150

Static Class Attributes and Inheritance

Avatar for Ram Sapkota

Ram Sapkota

October 28, 2018
Tweet

Transcript

  1. Static Class Attribute  Static Class attribute is an attribute

    about the class not about the specific objects of the class.  A static property is accessed not with instance name but after class name it self.  We will demonstrate it with a example  First lets make a class of student with needed properties, and make a constructor.  Now let’s add static attribute as below:
  2. Continue….  Now, lets initialize static class in our constructor

    as below:  Now let’s initialize and use this class as below in our main method.  Finally, you can see that the cunt increases as we create new class
  3. Static Methods & Classes  As of attribute Static Methods

    are the methods of the class not method of instances.  To create a static method just add static keyword in the method.  Static method is directly accesses like static attribute.  Static Class is a class of which we cannot create new instances.]  They are extremely useful.  Normally, static classes are used to include useful methods.  For Eg: Math is a static class and we cannot create instance of Math class.
  4. Inheritance  Inheritance in C# is a technique by which

    we can inherit all the properties and functions of one class to another.  Suppose we have a Person class with methods like Walks(), Sleeps() and Eat().  Now let’s make a student class, and we know that a student is also a person. Here Inheritance comes in to hand.  We can inherit properties and methods of Person class to Student Class by which Student class can access methods like Walks(), Sleeps()  To inherit we just use : after the Student class and write Person.  Similarly, we con override some methods of Person class also.  To do this just add virtual keyword to the required method.  Now in Student class just use override for that same method and you can now change the property of Person class.