1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Classes {
class Program {
abstract class abstracto {
public class embedded {
public void NotStatic() {
Console.WriteLine("You need an object of abstracto.embedded to call me.");
}
};
public static void VeryStatic(){
Console.WriteLine("You don't need an object of anything to call me.");
}
};
abstract class abstracti {
public void NotStatic(){
Console.WriteLine("You can only call me if you have an object of a class that inherits my class.");
}
}
class test : abstracti {
}
static void Main(string[] args) {
// Making an instance of a class inside an abstract class
abstracto.embedded aa = new abstracto.embedded();
aa.NotStatic();
/* This is invalid because embedded is not abstract and NotStatic is, well, not static:
* abstracto.embedded.NotStatic();
*/
// You can call a static method inside an abstract class without an object of that class:
abstracto.VeryStatic();
// Creating an instance/object of a class that inherits an abstract class and thus has all its public/protected members
test x = new test();
x.NotStatic();
/* This is illegal:
* abstracti.NotStatic();
* Because non-static members needs an instance of their parent class to be called; and because its in an abstract class,
* you cannot make an object of its class. But you can make an object of type test which inherits the abstracti class.
* */
Console.Read();
}
}
}
|