Difference between revisions of "Abstract - Virtual - Static"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Abstraction)
 
(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
<h2>Static</h2>
+
=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.
 
The "static" modifier means that a class cannot be instantiated, and the subroutines must instead be called directly.
  
if you had a class of human and created an instance of the human called wayne, the static modifier doesn't allow access such as wayne.example() , or wayne.example = 5, or Console.WriteLine(wayne.example). They are not avialable in an instance of a class.
+
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:
 +
<syntaxhighlight lang="csharp">
 +
wayne.example()
 +
 
 +
wayne.example = 5
 +
 
 +
Console.WriteLine(wayne.example)
 +
</syntaxhighlight>
 +
 
 +
They are not available in an instance of a class, but they are available without creating an object:
 +
 
 +
<syntaxhighlight lang="csharp">
 +
human.example()
 +
 
 +
human.example = 5
 +
 
 +
Console.WriteLine(human.example)
 +
</syntaxhighlight>
 +
 
 +
=Abstract=
 +
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.
 +
 
 +
<tabber>
 +
C#=
 +
<syntaxhighlight lang="csharp" line>
 +
class Animal {
 +
    //Constructor for Animal Class
 +
    public Animal(name, age, gender) {
 +
        this.name = name;
 +
        this.age = age;
 +
        this.gender = gender;
 +
    }
 +
 
 +
    public abstract void Output();
 +
 
 +
    private int _age;
 +
    private String _name;
 +
    private String _gender;
 +
 
 +
    public int age { get { return _age; } set { _age = value; } }
 +
    public String name { get { return _name; } set { _name = value; } }
 +
    public String gender { get { return _gender; } set { _gender = value; } }
 +
}
 +
 
 +
class Eagle : Animal {
 +
  public void Output() {
 +
      Console.WriteLine("{0} Is a {1} & is {2}",
 +
            this.name, this.gender, this.age,
 +
            "Years Old & doesnt walk but instead flies")
 +
  }
 +
}
 +
 
 +
class Tiger : Animal {
 +
    public void Output() {
 +
        Console.WriteLine("{0} Is a {1} & is {2} Years Old",
 +
            this.name, this.gender, this.age)
 +
    }
 +
}
 +
 
 +
</syntaxhighlight>
 +
</tabber>
 +
 
 +
So in the example above, the Output method of the Animal class is declared Abstract and no implementation is provided. Now each subclass must provide the implementation for this, so Tiger has an Output method implemented and so does Eagle.
 +
 
 +
=Virtual=
 +
A virtual method is one which is inheritable and can be overridden. So any method in the base class which is marked Virtual can be overridden in the subclass, and replaced with the subclass's own implementation of the method.
 +
 
 +
<tabber>
 +
C#=
 +
<syntaxhighlight lang="csharp" line>
 +
class Animal {
 +
    public Animal(name, age, gender) {
 +
        this.name = name;
 +
        this.age = age;
 +
        this.gender = gender;
 +
    }
 +
 
 +
    public virtual void Output() {
 +
        Console.WriteLine("{0} Is a {1} & is {2} Years Old",
 +
            this.name, this.gender, this.age)
 +
    }
 +
 
 +
    private int _age;
 +
    private String _name;
 +
    private String _gender;
 +
 
 +
    public int age { get { return _age; } set { _age = value; } }
 +
    public String name { get { return _name; } set { _name = value; } }
 +
    public String gender { get { return _gender; } set { _gender = value; } }
 +
}
 +
 
 +
class Eagle : Animal {
 +
  public override Output() {
 +
      Console.WriteLine("{0} Is a {1} & is {2}",
 +
            this.name, this.gender, this.age,
 +
            "Years Old & doesnt walk but instead flies")
 +
  }
 +
}
 +
 
 +
class Tiger : Animal {
 +
   
 +
}
 +
 
 +
</syntaxhighlight>
 +
</tabber>
 +
 
 +
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:
 +
# In Eagles case the overriden Output function will execute, as it is the latest definition of it in the hirearchy chain.
 +
# 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:
 +
 
 +
<tabber>
 +
C#=
 +
<syntaxhighlight lang="csharp" line>
  
<h2>Abstraction</h2>
+
Tiger tiger = new Tiger("Atsushi", 17, EGender.Male); // New Instance of Tiger
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.
+
Eagle eagle = new Eagle("Torway", 14, EGender.Male); // New Instance of Eagle
 +
 +
tiger.Output(); // Call to base function in parent Animal
 +
// Outputs "Atsushi is a Male and is 17 Years old"
 +
//-- Note this is the function found in the base class
 +
 +
eagle.Output(); // Call to base function in parent Animal
 +
// Outputs "Torway is a Male and is 14 Years old & doesnt walk but instead flies"
 +
//-- Note this is the function defined in the child class
  
<h2>Virtual</h2>
+
</syntaxhighlight>
A virtual method is one which is inheritable and can be overridden.
+
</tabber>

Latest revision as of 10:51, 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()

wayne.example = 5

Console.WriteLine(wayne.example)

They are not available in an instance of a class, but they are available without creating an object:

human.example()

human.example = 5

Console.WriteLine(human.example)

Abstract

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.

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

So in the example above, the Output method of the Animal class is declared Abstract and no implementation is provided. Now each subclass must provide the implementation for this, so Tiger has an Output method implemented and so does Eagle.

Virtual

A virtual method is one which is inheritable and can be overridden. So any method in the base class which is marked Virtual can be overridden in the subclass, and replaced with the subclass's own implementation of the method.

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

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