Inheritance is the key principal in object orientated programming. It allows one class to pass on properties and methods to child classes.
In a nutshell inheritance allows a class to inherit methods, properties and fields from another class and to also build upon its functionality. Classes inherit from another class using a semicolon followed by the base class on the class declaration line as seen on the line Class B : A. Class B inherits all the functionality of the base class, A, and adds additional functionality. The base class is the parent. In these examples I use an integer field for simplicity, but the same holds true for any other data type, method or property.
{
public int TestFieldA;
public int TestFieldB;
}
public class TestClassB : TestClassA
{
public int TestFieldC;
}
public void Main()
{
TestClassA testClassA = new TestClassA();
TestClassB testClassB = new TestClassB();
testClassA.TestFieldA = 1;
testClassA.TestFieldB = 2;
// Not allowed as TestClassA does not contain a field called TestFieldC
testClassA.TestFieldC = 3;
// TestClassB contains all of the fields from TestClassA
testClassB.TestFieldA = 4;
testClassB.TestFieldB = 5;
// As well as its own TestFieldC
testClassB.TestFieldC = 6;
}
Static
Each new object created has its own unique values. testClassA.TestFieldA is totally separated from testClassB.TestFieldA. The only exception is when a field is marked as static, in which case that field is shared between all objects of that class.
{
static int TestStatic = 0;
public int TestField = 0;
}
public void Main()
{
TestClass testClassA = new TestClass();
TestClass testClassB = new TestClass();
testClassA.TestField = 123;
testClassA.TestStatic = 456;
Console.WriteLine(testClassB.TestField); // 0
Console.WriteLine(testClassB.TestStatic); // 456
testClassB.TestField = 987;
testClassB.TestStatic = 555;
Console.WriteLine(testClassA.TestField); // 123
Console.WriteLine(testClassA.TestStatic); // 555
}
Static fields are used when a value is common to the class, not the object. The classic example is a bank account interest rate. All instances of the bank account class have their own ballance and so on, but they all share the same interest rate. Since the "static" interest rate field in shared between all bank account classes you can just set it once and it affects all classes.
The class can also be made static, which means that the class can be used without creating an instance. You will need to make all the properties, fields and methods of the class static if you wish to use them without creating an object. Notice that we are not creating an instance of the testClass with the new keyword.
{
public int testA;
private int testB;
static int testC;
}
static void Main()
{
// Not allowed as their is no instance of testClass.
testClass.testA = 1;
// Not allowed, as above and b is private so you
// will never access b outside the class.
testClass.testB = 2;
// Allowed. Both the class and field are static, so
// they can be used.
testClass.testC = 3;
}
Static methods can only call other static methods, or access static properties and fields.
{
public int testInt;
static int myTestMethod1
{
return 12345;
}
static int myTestMethod2
{
// This code will not compile as it accesses a non-static field.
return testInt;
}
}
static void Main()
{
Console.WriteLine(testClass.myTestMethod1();
Console.WriteLine(testClass.myTestMethod2();
}
No comments:
Post a Comment