Showing posts with label inheritance. Show all posts
Showing posts with label inheritance. Show all posts

December 31, 2013

Advanced CSharp Part 2 : Structs and Interfaces

We shall continue on more advanced features on CSharp. We have already seen how to use classes on different circumstances and how to modify its behavior under certain restraints; in this post we shall see two more data types that provide "class" type facilities, yet very different in nature and usages.

Struct: We start with structs. If you are familiar with C or C++, you are already acquainted with structs. Structs are similar to classes, but comes with some important differences.
  • Classes are reference types, while structs are value types.
  • They do not support inheritance. They do not derive (subclass) other struct types.
  • It is not possible to initiate a variable inside the struct definition. You have to do the initialization outside.
 The essential theme of using either of the class or a struct is the same: you want to keep logically relevant data together. So how do we understand when to use class or struct? We use struct when we have a small and simple data to handle. You just really don't need to mess up with classes when you need to handle just two or three variables and member fields. structs also allow properties and constructors like classes do. Let's see a working example.

using System;

namespace Spells
{
    public struct MyPoint
    {
        int x;
        int y;
        public MyPoint(int _x, int _y)
        {
            x = _x;
            y = _y;
        }
        public int Xcord 
        { 
            get { return x; }
            set { x = value; }
        }
        public int Ycord 
        {
            get { return y; }
            set { y = value; }
        }
    }
    class Program
    {
        public static void Main(string[] args)
        {
            MyPoint point = new MyPoint(4, -7);
            Console.WriteLine("point X = {0}, Y = {1}", point.Xcord, point.Ycord);

            MyPoint npoint = new MyPoint();
            npoint.Xcord = 64;
            npoint.Ycord = 45;
            Console.WriteLine("npoint X = {0}, Y = {1}", npoint.Xcord, npoint.Ycord);

            Console.Read();
        }
    }
}
Line 5 is no different than what you used to do while using classes. The only thing different is the keyword struct instead of class. I have used two variables of int types to indicate the x and y coordinates. Line 9 is a constructor that takes two arguments. Line 14 and 19 are two properties with getter and setter fields. I have created two instances of this struct inside the Main method. Consider line 29 and 32. You will see two different ways of instantiating struct type variables. Note that I have provided only a constructor that takes two arguments (see line 9). But still I am able to instantiate npoint at line 32 that takes no arguments. You can't really do that with classes.

You could still use a class instead of struct. But you use a struct when you see that you have lesser data to handle and you do not need additional methods to handle logic inside your program.

Interface: An interface is essentially a class. It's a close cousin of abstract class. Interfaces provide a way of grouping a set of common behaviors in a single place. They provide a specification rather than an implementation. If you are not familiar with abstract classes, you can get an idea in the last post. Consider the building codes class that we used earlier. If it happens that there are certain more regulations that builders must follow, then it will create a problem in your program because you cannot inherit more than one abstract class. But classes can in fact inherit multiple interfaces. Interfaces are more like behaviors. You implement these behaviors to the classes whoever wish to implement your interface.

using System;

namespace Spells
{
    interface IUtilities /* it is a convention to start interface name with 'I' */
    {
        bool Gas();         /* no method implementation logic here */
        bool Water();
        bool Electricity();
    }
    interface ICommercial
    {
        void Commercial();
    }
    interface IHousehold
    {
        bool Housing();
    }
    public class Hotel : IUtilities, ICommercial  /* multiple interfaces */
    {
        public bool Gas()
        { //codes here
            return true;
        }
        public bool Water()
        { //codes here
            return true;
        }
        public bool Electricity()
        { //codes here
            return true;
        }
        public void Commercial() { /*codes*/ }
        // other methods and members
    }
    public class MyHome : IUtilities, IHousehold
    {
        public bool Gas()
        { //codes here
            return true;
        }
        public bool Water()
        { //codes here
            return true;
        }
        public bool Electricity()
        { //codes here
            return true;
        }
        public bool Housing()
        {
            return true;
        }
    }
    class Program
    {
        public static void Main(string[] args)
        {
            
            Console.Read();
        }
    }
}
Build this code (F6). You will get a successful build. Line 5, 11 and 15 shows interface definition. It is a convention to name an interface with an uppercase 'I'. You may choose not to do so. Inside the interfaces definition, observe that there is no logic implementation here. You just specify which methods the implementing classes must implement. 

Line 19 and 36 shows how to implement interfaces in a class. The most important point is that you are able to implement multiple interfaces separated by comma. Also observe that you have to use the keyword public in front of the implementing methods. Go ahead and erase one of the methods from Hotel class. Your code will fail to build with an error.

To sum up, interfaces are nice way to force you to implement certain functionalities inside your code. An alternative is abstract class. But if you have multiple classes to inherit, you better make them interfaces and implement them as you can see above. 

December 14, 2013

More on Classes and Access Modifiers : Inheritance

In the previous post, we saw how to create our own class and how to work with it. In this post, we shall explore more on classes and access modifiers like private, protected and public.

One of the most powerful feature of object oriented programming is inheriting information from another class. This feature is commonly named as inheritance. Like real world example, if your parents or your grandparents leave behind something (wealth, land/property) after their death, then as a heir you own that wealth. You are eligible to consume that wealth because you have an inherited relation with the people leaving behind that wealth.

Apply the same on classes. A class can inherit part or all of the information that belongs to another class. Suppose you have a class called A. You write another class B which inherits A. Then we call B a subclass, while A is the base class. Lets learn these things modifying the previous example.

using System;

namespace Spells
{
    class Program
    {
        static void Main(string[] args)
        {
            PublicExam pe1 = new PublicExam("Nabiha", 5, 12, 2002, 2013);

            Console.Read(); //keeps the console alive
        }
    }
    class StudentInfo
    {
        string name;
        int standard;
        int rollnum;
        int dob;    //newly added dob -> date of birth
        static int count = 0;
        public StudentInfo(string name, int stndrd, int roll,int dob)
        {
            this.name = name;
            standard = stndrd;
            rollnum = roll;
            this.dob = dob;
            count++;
        }
        public static int GetCount()
        {
            return count;
        }
    }
    class PublicExam : StudentInfo
    {
        const string PRIMARY = "Primary";
        const string JUNIOR = "Junior";
        const string SECONDARY = "Secondary";
        const string H_SECONDARY = "Higher Secondary";
        int currentyear;
        public PublicExam(string name, int stndrd, int roll,int dob,int year)
            : base(name, stndrd, roll, dob)
        {
            currentyear = year;
        }
    }
}

I have changed the code quite a lot. If you compare the code from the previous post, you will see that a new class called PublicExam now inherits StudentInfo class. Therefore StudentInfo is the base class while PublicExam is a subclass.

Notice line 34. This is the way you inherit a class. In this case, PublicExam class is inheriting StudentInfo class. Now have a good look inside the Main method. We are no longer creating objects of StudentInfo, rather we are interested in objects of PublicExam. In object oriented programming, you already know that when you create an object, you automatically call the constructor. In this case you are calling the PublicExam constructor which takes 5 arguments. This automatically makes a call to the base class constructor StudentInfo. Observe line 42, in this case you are actually specifying how to call the base class constructor. You can choose not to specify in this way; in that case you have to provide a base class constructor that takes no arguments. Go ahead and remove line 42. What do you see? An error that says "Error    1    'Spells.StudentInfo' does not contain a constructor that takes 0 arguments ". You can get rid of this error. For example -
using System;

namespace Spells
{
    class Program
    {
        static void Main(string[] args)
        {
            PublicExam pe1 = new PublicExam("Nabiha", 5, 12, 2002, 2013);

            Console.Read(); //keeps the console alive
        }
    }
    class StudentInfo
    {
        string name;
        int standard;
        int rollnum;
        int dob;    //newly added dob -> date of birth
        static int count = 0;
        public StudentInfo()
        { 
            //necessary codes here
        }
        public StudentInfo(string name, int stndrd, int roll,int dob)
        {
            this.name = name;
            standard = stndrd;
            rollnum = roll;
            this.dob = dob;
            count++;
        }
        public static int GetCount()
        {
            return count;
        }
    }
    class PublicExam : StudentInfo
    {
        const string PRIMARY = "Primary";
        const string JUNIOR = "Junior";
        const string SECONDARY = "Secondary";
        const string H_SECONDARY = "Higher Secondary";
        int currentyear;
        public PublicExam(string name, int stndrd, int roll,int dob,int year)
        {
            currentyear = year;
        }
    }
}

To sum up, if you do not provide a statement like base(...), the compiler will automatically call the base class constructor that has no argument. The constructor from line 21 to 24 takes no arguments. You would probably think that how two methods with the same name can co-exist? They can. This is called constructor overloading. Overloaded methods can have the same name, but they should differ on the number of arguments.
Does this code serve to our purpose? Though the base class constructor is called, but it is actually of no use to us, at least in this case. So we better specify how to call. That was what the first code was doing for us. We revert back to that again. Now we make the classes useful. You possibly have the students date of birth and which standard he/she reads in. Suppose when someone is in standard 5 and at least 10 years old, he/she is eligible to enter Primary exam. How do we do that?

using System;

namespace Spells
{
    class Program
    {
        static void Main(string[] args)
        {
            PublicExam pe1 = new PublicExam("Nabiha", 5, 12, 2000, 2013);

            Console.Read(); //keeps the console alive
        }
    }
    class StudentInfo
    {
        string name;
        int standard;
        int rollnum;
        int dob;    //newly added dob -> date of birth
        static int count = 0;
        public StudentInfo(string name, int stndrd, int roll,int dob)
        {
            this.name = name;
            standard = stndrd;
            rollnum = roll;
            this.dob = dob;
            count++;
        }
        public static int GetCount()
        {
            return count;
        }
    }
    class PublicExam : StudentInfo
    {
        const string PRIMARY = "Primary";
        const string JUNIOR = "Junior";
        const string SECONDARY = "Secondary";
        const string H_SECONDARY = "Higher Secondary";
        int currentyear;
        public PublicExam(string name, int stndrd, int roll,int dob,int year): base(name,stndrd,roll,dob)
        {
            currentyear = year;
        }

        public string WhichExam()
        {
            int age = currentyear - dob;   //the current age
            if (standard == 5 && age >= 10)
            {
                return PRIMARY;
            }
            else if (standard == 8 && age >= 13)
            {
                return JUNIOR;
            }
            else if (standard == 10 && age >= 16)
            {
                return SECONDARY;
            }
            else if (standard == 12 && age >= 18)
            {
                return H_SECONDARY;
            }
            else
            {
                return "No exams!";
            }
        }
    }
}
This code will let you down. You will see that dob and standard variables aren't recognized inside the function WhichExam. Your Visual Studio should say that these variables aren't accessible because of protection level. But hey! Didn't we just inherit everything from StudentInfo? True. Then what's wrong? In C#, everything by default is private. So all the variables inside StudentInfo are private and are only accessible inside StudentInfo class. So how do we access these variables? There are two possible ways -
  • Make the variables public.
  • Make the variables protected.
I will go with solution no. 2. Making the variables public isn't an intelligent solution. Because in that way, you expose your variables to the outer world that aren't quite necessary to them. You also make them vulnerable because you can accidentally modify their values anywhere else! See the working code using protected modifier on line 19 and 21.

using System;

namespace Spells
{
    class Program
    {
        static void Main(string[] args)
        {
            PublicExam pe1 = new PublicExam("Nabiha", 5, 12, 2000, 2013);
            PublicExam pe2 = new PublicExam("Abrar", 2, 22, 2003, 2013);
            Console.WriteLine("{0} will sit for {1}",pe1.GetStudentName(),pe1.WhichExam());
            Console.WriteLine("{0} will sit for {1}", pe2.GetStudentName(), pe2.WhichExam());
            Console.Read(); //keeps the console alive
        }
    }
    class StudentInfo
    {
        string name;
        protected int standard;
        int rollnum;
        protected int dob;    //newly added dob -> date of birth
        static int count = 0;
        public StudentInfo(string name, int stndrd, int roll,int dob)
        {
            this.name = name;
            standard = stndrd;
            rollnum = roll;
            this.dob = dob;
            count++;
        }
        public static int GetCount()
        {
            return count;
        }
        public string GetStudentName()
        {
            return name;
        }
    }
    class PublicExam : StudentInfo
    {
        const string PRIMARY = "Primary Exam";
        const string JUNIOR = "Junior Exam";
        const string SECONDARY = "Secondary Exam";
        const string H_SECONDARY = "Higher Secondary Exam";
        int currentyear;
        public PublicExam(string name, int stndrd, int roll,int dob,int year) : base(name,stndrd,roll,dob)
        {
            currentyear = year;
        }

        public string WhichExam()
        {
            int age = currentyear - dob;   //the current age
            if (standard == 5 && age >= 10)
            {
                return PRIMARY;
            }
            else if (standard == 8 && age >= 13)
            {
                return JUNIOR;
            }
            else if (standard == 10 && age >= 16)
            {
                return SECONDARY;
            }
            else if (standard == 12 && age >= 18)
            {
                return H_SECONDARY;
            }
            else
            {
                return "No exams!";
            }
        }
    }
}
Aha! Your code is ready to run. Hit F5 and see the output. Finally, you will notice that you can access methods from the base class by an object of the subclass. Subclass objects can have access to public methods inside the base class.

I hope the concepts of inheritance, access modifiers and how to control access to members and methods are quite clear. Just play with them, edit/modify these codes and see what you are allowed to do and what not.