Guys , Hope you are doing well , Today i am again back with some good article , today i will try to write something which i think is one of the favorite topic of all time for an interviewer.
So Inheritance , Most of you knows what is inheritance but the actual in depth knowledge of inheritance is very much required while you are preparing for interview.
In my earlier post I have talked about Tower of Hanoi problem
You can also see other Sorting program
Bubble Sort
Selection Sort
Insertion Sort
So Inheritance , Most of you knows what is inheritance but the actual in depth knowledge of inheritance is very much required while you are preparing for interview.
In my earlier post I have talked about Tower of Hanoi problem
You can also see other Sorting program
Bubble Sort
Selection Sort
Insertion Sort
- What is Inheritance ?
- In general - acquiring the properties and character tics of parent by child.
Class A // parent class
{ void m1();
void m2();
}
Class B extends A // child class(class B has method m1 & m2 available)
{
void m3();
void m4();
}
ADVANTAGES OF INHERITANCE
-reduce the redundance
- reduce the length of code;
NOTE - One class only extends one class (not more than one class)
B b = new B(); // we can access 4 method using b object;
A a = new A(); // only access 2 methods;
Interview Question - recommended to create the object of parent or child class ??
ans - child - bcz it is possible to access parent method also;
NOTE - root class is the class which is not extending any class.
-- root class of all java classes -> Object class (package java.lang )
TYPE OF INHERITANCE-
- -single level inheritance
- -multiple inheritance
- -multilevel inheritance
- -hirarical inheritance
- -hybrid inheritance
---SINGLE LEVEL INHERITANCE---
when a class is extending only one parent class and the class is not extended by any other class
Ex-
Class A {
void m1()
}
Class B extends A
{
void m2()
}
---multilevel inheritance
Ex-
Class A {
void m1()
}
Class B extends A{
void m2()
}
Class C extends B {
void m3()
}
--heirarical inheritance
Ex
Class A
{
}
Class B extends A
{
}
Class C extends A
{
}
Class D extends A
{
}
--multiple inheritance
Ex
Class A
{
void money ()
}
Class B
{
void money ()
}
Class C extends B, A
{
}
NOTE - now when c is trying to access , then obviously ambiguity occured for c object which method to call(SO IT IS NOT SUPPORTED BY JAVA)(diamond problem )
--HYBRID INHERITANCE = MULTIPLE + HEIRARICAL (java is not supporting multiple so java is not supporting hybrid)
In Next Article we will have Inheritance in depth article , If you have any problem in this post , do let me know , I will try to solve your problem , Do Comment in comment section .
Thanks for Reading
-Noeik
0 comments:
Post a Comment