The main focus of OOP programming language is reducing to write code again and again. Is it possible; once you write a code and you can call that it anywhere in your program whenever you required.
Yes, you can do this type of work and it's power of OOP programming language. Once you create a class and define some methods over there then you can call this class inside of other class and use its methods or properties. This mechanism of designing or constructing one class form another class is called Inheritance.
What is Inheritance : When ever one class wants to access properties, methods of another class or to modify methods of another class or implement methods inside, it can possible only because of Inheritance .
Type of Inheritance:
I think before understand Inheritance we should understand visibility and its importance. As it name automatically say a lot about itself, but we can say visibility is a technique by using you can access of one method into another class.
We can define visibility in two way :
Yes, you can do this type of work and it's power of OOP programming language. Once you create a class and define some methods over there then you can call this class inside of other class and use its methods or properties. This mechanism of designing or constructing one class form another class is called Inheritance.
What is Inheritance : When ever one class wants to access properties, methods of another class or to modify methods of another class or implement methods inside, it can possible only because of Inheritance .
Type of Inheritance:
- Single inheritance
- Multiple inheritance
- Hierarchical inheritance
- Multilevel inheritance
I think before understand Inheritance we should understand visibility and its importance. As it name automatically say a lot about itself, but we can say visibility is a technique by using you can access of one method into another class.
We can define visibility in two way :
- Class Visibility
- Methods Visibility
Methods Visibility: Method visibility set with the keyword public , private, protected and etc...
Base Class Constructors :
Constructors are come in picture when we want to put values to the data fields of the class. constructor invoked when we create the object of the class. constructor are different types and in OOP programming language its huge impact in the program.
When one class inherit another class ( B inherit class A), and we wants to create object of class B. is it possible the same time we call of constructor of class A without create its object. Yes! we can, then the Base class constructors comes in the picture.
Exp:
Class A
{
public int a;
public int b;
public A (int x, int y) // constructor for class A
{
a= x;
b= y;
}
public int ADD( ) // addition method here
{
return ( a + b) ;
}
}
Class B : A // inherit class A
{
int c;
public B ( int x, int y, int z) : base(x ,y)
{
c=z;
}
// note base keyword used here for calling class A constructor inside class B. first two values (x,y) put in base( x,y) which call base class constructor and put values in a, b
public Multiple( ) // Multiple method here
{
return (c * a* b);
}
}
When you create the object of class B with the values, same time base class constructor all and initialize values of class A data fields.
* Note: base class constructor call before the derived class constructor.
No comments:
Post a Comment