Difference between revisions of "Abstract - Virtual - Static"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Abstraction)
(Abstraction)
Line 20: Line 20:
  
 
=Abstraction=
 
=Abstraction=
The "abstract" modifier when declaring a class means that the class contains one or more abstract subroutine, and must have one or more subclasses for implementations of abstract methods. A subclass of an abstract class that is instantiated must implement any abstract subroutines contained in the abstract class.
+
A simple definition would be, a method marked Abstract is declared in the base class, but provided with the code in the sub class. So the method will be declared in the base class but no code will be provided and the method will not be implemented, the sub class will actually provide the code and implementation of what this method does. Any sub class must implement all abstract methods contained in the base class, failure to do so will prevent generate an exception.
 
 
A simpler definition would be a method marked Abstract is declared in the base class, but provided with the code in the sub class. Any sub class must implement all abstract methods contained in the base class.
 
  
 
=Virtual=
 
=Virtual=

Revision as of 10:28, 1 July 2020

Static

A static method belongs to the class rather than any object of a class. So they can be accessed without having to create a new object.

The "static" modifier means that a class cannot be instantiated, and the subroutines must instead be called directly.

Imagine you had a class of human and created an instance of the human called wayne. If the class contains a static method called example then this doesn't allow access such as:

wayne.example()

or:

wayne.example = 5

or:

Console.WriteLine(wayne.example)

They are not available in an instance of a class.

Abstraction

A simple definition would be, a method marked Abstract is declared in the base class, but provided with the code in the sub class. So the method will be declared in the base class but no code will be provided and the method will not be implemented, the sub class will actually provide the code and implementation of what this method does. Any sub class must implement all abstract methods contained in the base class, failure to do so will prevent generate an exception.

Virtual

A virtual method is one which is inheritable and can be overridden.

Extra Detail

The propper etiquette for defining methods we want to be overriden is to mark them as virtual, and then have any child classes which may require them in a specific way to override them.


In it's simplest form, the virtual specifier marks a function as being fine as it is however if a child class you've made requires something more specific you may override it. Once a virtual method is called, the CSharp compiler will look for the first instance of said method existing with a body (namely being overriden) and runs it, starting it's search from the class instance being called all the way to the parent class (where it must be defined at least once). Counter to the virtual and override route to implementing specified inheritence you can also mark a method as abstract, meaning it's too general an attribute to be defined in a base class and must be set for each child class despite being required in every class which may inherit it (to do this, abstract methods are defined without a body to prevent the CSharp compiler executing an instance of it found in the base class); for example if an Animal class possesses a Walk method, the way a Animal walks is almost always inextricably linked to the type of animal it is and hence must be defined for each class which inherits it. You can remember the difference between virtual and abstract methods as abstract only posessing a body when inherited while virtual always has at least one body to be run regardless of where it's declared. To display this I will re-create the above Animal class as an example (in an abridged form):

 1 private enum EGender {
 2   Male, female
 3   // Enumeration containing
 4 }; // possible gender values
 5 
 6 class Animal {
 7     public Animal(name, age, gender) {
 8         this.name = name; 
 9         this.age = age;
10         this.gender = gender;
11     }
12 
13     public virtual void Output() {
14         Console.WriteLine("{0} Is a {1} & is {2} Years Old",
15             this.name, this.gender, this.age)
16     }
17 
18     private int _age;
19     private String _name;
20     private EGender _gender;
21 
22     public int age { get { return _age; } set { _age = value; } }
23     public String name { get { return _name; } set { _name = value; } }
24     public EGender gender { get { return _gender; } set { _gender = value; } }
25 }
26 
27 class Eagle : Animal {
28    public override Output() {
29        Console.WriteLine("{0} Is a {1} & is {2} {3}",
30             this.name, this.gender, this.age,
31             "Years Old & doesnt walk but instead flies")
32    }
33 }
34 
35 class Tiger : Animal {
36     
37 }

Taking a look at the code above you can see Eagle & Tiger both inherit from Animal but only Eagle overrides the virtual Output function and Tiger leaves the Output function as is. Now as for what happens when instances of each class call the Output function:

  1. In Eagles case the overriden Output function will execute, as it is the latest definition of it in the hirearchy chain.
  2. In Tigers case there is no overriden function and hence the CSharp compiler will travely up the hirearchy chain to the base class and run the function body found there.

To make sure however just run the code below:

 1 Tiger tiger = new Tiger("Atsushi", 17, EGender.Male); // New Instance of Tiger
 2 Eagle eagle = new Eagle("Torway", 14, EGender.Male); // New Instance of Eagle
 3  
 4 tiger.Output(); // Call to base function in parent Animal
 5 // Outputs "Atsushi is a Male and is 17 Years old"
 6 //-- Note this is the function found in the base class
 7  
 8 eagle.Output(); // Call to base function in parent Animal
 9 // Outputs "Torway is a Male and is 14 Years old & doesnt walk but instead flies"
10 //-- Note this is the function defined in the child class