Inheritance



Inheritance

In the programming language, It is derive from the parent class to other sub classes. here we can easily reuse the codes in programming. normally "extends","implements" keywords are used in the programming.





This class is inheriting the data and behaviors from one class to another class.

Vehicle---------------->Car
Car------------------->Speed Car

Example code for the Inheritance Concept


public class ParentClass
    {
        public ParentClass()
        {
            Console.WriteLine("Parent Constructor.");
        }

        public void print()
        {
            Console.WriteLine("I'm a Parent Class.");
        }
    }

    public class ChildClass : ParentClass
    {
        public ChildClass()
        {
            Console.WriteLine("Child Constructor.");
        }

        public static void Main()
        {
            ChildClass child = new ChildClass();

            child.print();
        }
    }



Output:
    Parent Constructor.
    Child Constructor.
    I'm a Parent Class.




Encapsulation

 This one of the basic concept of OOP. this is approach to make security to the class, such as private or public. so in this encapsulation it can hide the fields in the class..

Example code for the Encapsulation Concept

class Demo
{
   private int _mark;

   public int Mark
   {
     get { return _mark; }
     set { if (_mark > 0) _mark = valueelse _mark = 0; }
   }
 }




Abstraction

Normally abstract means eliminate, here show only the necessary details and hide other details. abstract also is a keyword in java.
Eg:- In this picture, we can access the iPhone but we didn't know about the inside functions , circuit  and other things. like wise in the abstract concept it's show the appropriate details and hide the inappropriate details.

Example code for the Abstraction Code

    abstract class MobilePhone
    {
        public void Calling();
        public void SendSMS();
    }

    public class Nokia1400 : MobilePhone
    {

    }

    public class Nokia2700 : MobilePhone
    {
        public void FMRadio();
        public void MP3();
        public void Camera();
    }

    public class BlackBerry : MobilePhone
    {
        public void FMRadio();
        public void MP3();
        public void Camera();
        public void Recording();
        public void ReadAndSendEmails();

    }





Polymorphism

Basically, poly means many and morph means behavior or forms so many behaviors in programming. same functionality are same object which has many behaviors.

Example Code for Ploymorphism Concept

class DemoOverload{

    public int add(int x, int y){  //method 1

    return x+y;

    }

    public int add(int x, int y, int z){ //method 2

    return x+y+z;

    }

    public int add(double x, int y){ //method 3

    return (int)x+y;

    }

    public int add(int x, double y){ //method 4

    return x+(int)y;

    }

}

class Test{

    public static void main(String[] args){

    DemoOverload demo=new DemoOverload();

    System.out.println(demo.add(2,3));      //method 1 called

    System.out.println(demo.add(2,3,4));    //method 2 called

    System.out.println(demo.add(2,3.4));    //method 4 called

    System.out.println(demo.add(2.5,3));    //method 3 called

    }

}


There are two common ways of implementing polymorphism.
1. Overloading
2. Overriding

Overloading
Using the same method name with different parameter types lists.

Overriding
Using the different implementations of the same method in sub classes.