-
Notifications
You must be signed in to change notification settings - Fork 1
Lesson2
References for Lesson 2 including examples:
- Introduction to Class
- Introduction of multiple constructors (Also known as function/constructor overloading)
- Introdurction to static keyword
- Inheritance and Access Modifier (public, protected, private)
- Virtual Keyword
- Summary
A class is a template that you as a programmer create for
Go to File -> New -> Project and select Console App under Visual C# section.
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Now right click the ConsoleApp1 (Or whatever name you gave when you create the project), then Add -> New Folder and name it whatever you want.
Next, right click on the folder that you've created, Add -> Class... and name it whatever you want. (In my case I name it as MainPage)
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1.NewFolder
{
class MainPage
{
//Here lies the body of your class
}
}
Now add fields/methods in the class.
Don't worry about "public" for now, it will be explained later on.
class MainPage
{
public string Title = ("This is a title");
public string Content = ("This is a content");
public void SetTitle(string S)
{
Title = S;
}
public string GetTitle()
{
return Title;
}
}
We will now use the freshly create class, MainPage
Now add var Page1 = new MainPage();
inside your Main.
When adding that particular line, you will notice that it shows a red swiggly line under the MainPage().
Now press ctrl + ., and choose "Import missing references in file" from the drop down list and you will notice that using ConsoleApp1.NewFolder;
is added under using System;
. This means that the folder that has the class, MainPage is included in Program.cs.
class Program
{
static void Main(string[] args)
{
var Page1 = new MainPage();
MainPage Page2 = new MainPage();
//Creating two object based on the class MainPage aka the template
//Both of them are the same just different way of creating (Check Lesson1 if you don't know what is var)
Console.WriteLine(Page1.Title);//Displaying the value of Title
Console.WriteLine(Page1.Content);//Displaying the value of Content
Console.WriteLine(Page2.Title);//Displaying the value of Title
Console.WriteLine(Page2.Content);//Displaying the value of Content
//You will notice that both Page1 and Page2 are displaying the same thing.
//This is because both of them are created from the same exact template of the class MainPage.
Page1.SetTitle("New Title for Page1");
Page2.SetTitle("New Title for Page2");
//A method/function that changes/update the Title value of the object Page1
Console.WriteLine(Page1.Title);
Console.WriteLine(Page1.GetTitle());
//Here, you will find that both of them shows the same value because they are access the same variable/field aka Title. More on it later on when we discuss about public.
Console.WriteLine(Page2.Title);
}
}
Now, run the program. You will notice the program runs and instantly closes. This is because there is not a stopping point for the program the stop running.
Add a breakpoint as the image shown:
After the 2 SetTitle Line, you will find that both the Title of Page1 and Page2 are different . This is because even though both of them are created from the same template, they are basically different object.
By changing the Title of Page1, only the Title of Page1 will be changed and vice versa
Now, go to your MainPage class, and change to as follows:
class MainPage
{
public MainPage()
{
//This is your default constructor.
//Although it is not shown on the example above, your program will always create one no matter what. It is just not visible
Console.WriteLine("MainPage Created with default constructor");
}
public MainPage(string s)
{
//This is your overloaded constructor/ the other constructor that the program will call when you create a new MainPage object and provide it with a string.
Title = s;
Console.WriteLine($"MainPage Created with overloaded constructor with the Title as {Title}");
}
public string Title = ("This is a title");
public void SetTitle(string S)
{
Title = S;
}
public string GetTitle()
{
return Title;
}
}
class Program
{
static void Main(string[] args)
{
var Page1 = new MainPage();
var Page2 = new MainPage("Page2");
Console.WriteLine(Page1.Title);//Displaying the value of Title for Page1
Console.WriteLine(Page2.Title);//Displaying the value of Title for Page2
}
}
Here you will find that Page2 Title is displayed as Page2. This is because Page2 is created using the overloaded constructor with the string "Page2" provided.
The keyword static means that the variables/methods in a class will be shared across all object.
Static variable/method is called using <ClassName>.<VariableName/MethodName()>
Ex. Console.WriteLine()
Sometime we don't need to depend on an object to perform certain task Recall Console.WriteLine.
Yes, Console itself is a class, however there is no point in creating an object of Console in order to print something in the console.
We will demo the usage of static as below:
class MainPage
{
public string Title = "Page";
public static string Display = "This is just a demo";
public static void WriteLine(string s)
{
Console.WriteLine(s);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(MainPage.Display);
MainPage.Display = "Some Text";
Console.WriteLine(MainPage.Display);
MainPage.WriteLine("This is a demo of static method");
}
}
After running the code, you will see that the value from the static variable Display of MainPage is displayed without creating any object at all.
Second, you can also access the static method by just MainPage.WriteLine("<Insert anything here>")
.
If you were to try to access the static method, WriteLine through the object, the error below will be throwned
Now, add a new class in the same folder as MainPage.cs and call it whatever you want. (I will be using SubPage.cs here)
class MainPage
{
public string Title = "Page Title";
public void SetTitle(string s)
{
Title = s;
}
protected string Name = "Some Name";
private string Something = "Something else";
public string GetSomething()
{
return Something;
}
}
class SubPage : MainPage
{
public string Content = "Some Content";
public void SetContent(string s)
{
Content = s;
}
public string GetName()
{
return Name;
}
}
Notice the ":" between SubPage and MainPage. It means that the SubPage Is A MainPage aka, inherited from MainPage
class Program
{
static void Main(string[] args)
{
MainPage Page1 = new MainPage();
SubPage Page2 = new SubPage();
Debug.WriteLine(Page1.GetSomething());
Debug.WriteLine(Page2.GetSomething());
Debug.WriteLine(Page2.GetName());
//Demo of truncation when assigning sub class object to base class object.
SubPage Page3 = new SubPage();
MainPage TruncatedPage = Page3;
//You can do this too but TrucatedPage will only have fields/methods from the class MainPage even though TruncatedPage is an object of Page3 because they have been truncated when it is assigned.
}
}
After running the code, you will see that Page2.GetSomething()
will also return you the same value as in Page1.GetSomething()
even though there is no GetSomething() function in SubPage.
The next thing you will notice is even though SubPage doesn't have string Name created, you still can access the value of Name from MainPage.
All of these is done through inheritance. SubPage inherited most of the stuff from MainPage. Why most but not all?
This is because private field/method is not inherited from its parent/base/super class. Here is the error you will get if you try to access the value of private field of MainPage from SubPage:
Protected is inherited from the parent/base/super class, in this case MainPage. However, you cannot directly access from the object created from the sub class, SubPage. Here is the error you will get if you try to access the value of protected field inherited from MainPage through SubPage:
Even though SubPage is inherited from MainPage, you still cannot access the field created in SubPage from the object of MainPage. Here is the error you will get if you do so:
Error when you try to access truncated field/method:
Location of Access / Access Modifier | Public | Protected | Private |
---|---|---|---|
In Main or other Location | ✓ | ✗ | ✗ |
In Sub/Derived Class | ✓ | ✓ | ✗ |
In the class itself | ✓ | ✓ | ✓ |
To Be Added
Today's lesson summary:
- Create class
- Create fields, e.g. public string Title
- Create methods, e.g. public void SetTitle(string s)
- Create multiple constructors
- Create static field and methods e.g. public static string Name; public static string WriteLine(string s)
- Static method is something like Console.WriteLine
- inheritance
- access modifier (public, protected, private)
- virtual functions