well to say it very generally encapsulation is the inherit hierarchy and the access modifiers to the class.
abstraction is used in inherit to have a class you cant have a instance of. its generally used so you have a "base" class with the most important info. also an abstract class MUST be override by an child class.
for instance lets assume we wanna make a system containing informations about customers and workers in a store. then we can make a class like this:
public abstract class Person
{
public string firstname { get; protected set; }
public string lastname { get; protected set; }
public int phonenumber { get; protected set; }
public Person(string _firstname, string _lastname, int _phonenumber)
{
firstname = _firstname;
lastname = _lastname;
phonenumber = _phonenumber;
}
}
note that this class is abstract and can not have its own instance and will also act as our "super-class".
next up we create a customer inherit from "Person" so a customer will have at least the same functionality as a person.
public class Customer : Person
{
public string customerID { get; protected set; }
public Customer(string _customerID, string _firstname, string _lastname, int _phonenumber)
: base(_firstname, _lastname, _phonenumber)
{
customerID = _customerID;
}
}
note here that we're using the "base" keyword to say we use the "superclass" to have these items.
next we make a employee class also inherit from the person class.
public class Employee : Person
{
public int worktime { get; protected set; }
public Employee(int _worktime, string _firstname, string _lastname, int _phonenumber)
: base(_firstname, _lastname, _phonenumber)
{
worktime = _worktime;
}
}
now to the magic of polymorphism. you can now in you main method (or a collection/database class) make a list with persons and have both employees and customers in that list as seen below.
static void Main(string[] args)
{
List<Person> persons = new List<Person>() { new Employee(36, "claus", "dawnson", 12345678), new Customer("ge2tt64mg", "martin", "smith", 78901234) };
}
as you can see above we add both a employee and a customer in a Person list and that is basically the good thing about polymorphism.
if we expand our small program to also have some abstract and virtual methods that will be override in the inherit we'll get to a point where we will find really good use of our polymorphism. full sample here below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test
{
class Program
{
static void Main(string[] args)
{
List<Person> persons = new List<Person>() { new Employee(36, "claus", "dawnson", 12345678), new Customer("ge2tt64mg", "martin", "smith", 78901234) };
persons.OrderBy(p => p.lastname).ThenBy(p => p.firstname);
foreach (Person p in persons)
{
Console.WriteLine(p.ToString());
}
Console.ReadKey();
}
}
public abstract class Person
{
public string firstname { get; protected set; }
public string lastname { get; protected set; }
public int phonenumber { get; protected set; }
public Person(string _firstname, string _lastname, int _phonenumber)
{
firstname = _firstname;
lastname = _lastname;
phonenumber = _phonenumber;
}
public abstract override string ToString();
public virtual string GetCustomerID()
{
return null;
}
}
public class Customer : Person
{
public string customerID { get; protected set; }
public Customer(string _customerID, string _firstname, string _lastname, int _phonenumber)
: base(_firstname, _lastname, _phonenumber)
{
customerID = _customerID;
}
public override string ToString()
{
return firstname + " " + lastname + " " + phonenumber + " " + customerID;
}
public override string GetCustomerID()
{
return customerID;
}
}
public class Employee : Person
{
public int worktime { get; protected set; }
public Employee(int _worktime, string _firstname, string _lastname, int _phonenumber)
: base(_firstname, _lastname, _phonenumber)
{
worktime = _worktime;
}
public override string ToString()
{
return firstname + " " + lastname + " " + phonenumber + " " + worktime;
}
}
}
as you can see we have added a abstract tostring method that MUST be override in the child classes. You can add as many as you want either abstract or virtual. abstract have to be override i all child classes where virtual is not needed to be override but it can.
you can find A LOT more about this all over the internet and here on this site. Also i will recommend you to look at interfaces since they some times also can come in handy (see one of my old questions
Parse classes to interface that can inherit[
^])
lastly, thanks for reading and i hope you find this useful and sorry for my crappy English (danish is my native language). fell free to ask if you have any questions.
- Jackie
PS. the best way to learn it is to try it out by you self, try to make some simple databases, for example try to make a small library/store selling pc parts etc.