Inheritance.
Inheritance is a feature of oops, which provides flexibility to share data from base classes to derived class.
Inheritance (OOP) is when an object or class is based on another object (prototypal inheritance) or class (class-based inheritance), using the same implementation (inheriting from an object or class) specifying implementation to maintain the same behavior (realizing an interface; inheriting behavior).
Parent : base : super ...
Child : derived : sub...
Benefits of using the inheritance :
1) code re usability
How to implement the base class
How to implement the derivide classes
Initialize the base classes from dervived classs
Note : base class should not hold any information about child class.
Protected : Protected is similar access specifier as of private but the data present in the protected section can be inherited.
To inherit from one class to another class I will be using (:)
eg: class derived : base classname
{
}
Write a program to create area of a square, area of rectangle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
square s = new square();// this is child class of shappe
s.D1 = 10;
//s.areaofsquare(s.D1);// comment
// Console.Read();
Rectangle R = new Rectangle();
R.D1 = 10;
R.D2 = 20;
R.area(R.D1, R.D2);
Console.Read();
}
}
class shape
{
public void area(int d1, int d2)
{
Console.WriteLine(d1 * d2);
}
}
class square : shape
{
private int d1;
public int D1
{
get
{
return d1;
}
set
{
d1 = value;
}
}
public void areaofsquare(int s)
{
area(s, s);
}
}
class Rectangle : shape
{
private int d1, d2;
public int D1
{
get
{
return d1;
}
set
{
d1 = value;
}
}
public int D2
{
get
{
return d2;
}
set
{
d2 = value;
}
}
}
}
Inheritance (OOP) is when an object or class is based on another object (prototypal inheritance) or class (class-based inheritance), using the same implementation (inheriting from an object or class) specifying implementation to maintain the same behavior (realizing an interface; inheriting behavior).
Parent : base : super ...
Child : derived : sub...
Benefits of using the inheritance :
1) code re usability
How to implement the base class
How to implement the derivide classes
Initialize the base classes from dervived classs
Note : base class should not hold any information about child class.
Protected : Protected is similar access specifier as of private but the data present in the protected section can be inherited.
To inherit from one class to another class I will be using (:)
eg: class derived : base classname
{
}
Write a program to create area of a square, area of rectangle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
square s = new square();// this is child class of shappe
s.D1 = 10;
//s.areaofsquare(s.D1);// comment
// Console.Read();
Rectangle R = new Rectangle();
R.D1 = 10;
R.D2 = 20;
R.area(R.D1, R.D2);
Console.Read();
}
}
class shape
{
public void area(int d1, int d2)
{
Console.WriteLine(d1 * d2);
}
}
class square : shape
{
private int d1;
public int D1
{
get
{
return d1;
}
set
{
d1 = value;
}
}
public void areaofsquare(int s)
{
area(s, s);
}
}
class Rectangle : shape
{
private int d1, d2;
public int D1
{
get
{
return d1;
}
set
{
d1 = value;
}
}
public int D2
{
get
{
return d2;
}
set
{
d2 = value;
}
}
}
}
Inheritance.
Reviewed by Share less to Learn More
on
6:40 PM
Rating:
No comments