Thursday, January 12, 2017

Inheritance - Indepth- Important Topic of Interview of Java

Hi Guys, Yesterday I posted about Inheritance , what how and type of inheritance . today i am going to post articles about the Indepth knowledge of inheritance, So lets starts

HOW TO PREVENT THE CLASS FROM INHERITANCE
to prevent the class from inherit , we should use final modifier
Ex-
Class A{
}
Class B extends A{
}
OUTPUT - inheritance possible
final Class A{
}
Class B extends A
{
}
Output -inheritnace not possible as class is final (cannot inherit from final )

this keyword - to indicate the current object
super keyword is used to indicate the parent object of the current Object

USE OF SUPER KEYWORD IN INHERITANCE

Class Parent{

Parent()
{
syso("this is parent 0-arg constrctr");
}
}
Class Child extends Parent
{
Child()
{
this(10)    // to call child 1 args constructor
syso("child 0-arg constructor")
}
Child(int agr)
{
super()  // to call parent 0 args constructor
syso ("child 1-arg constructor");
}

psvm(string[] args)
{
new Child();
}
}
NOTE - SUPER & THIS CALL MUST BE FIRST STATEMENT IN CONSTRUCTOR
NOTE 2 - SUPER & THIS BOTH CANT BE POSSIBLE TO CALL IN SAME CONSTRUCTOR
NOTE 3- CHILD CLASS CONSTRUCTOR WILL ALWAYS CALL ANOTHER CONSTRUCTOR EITHER EXPLICIT OR IMPLICIT IF WE DON'T SPECIFY THE CONSTRUCTOR THEN SUPER() WILL BE CALLED

* compiler generated code always called zero argument constructor
* if we created one arg constructor then we will have to create zero arg constructor else it will generate compiler error

Instance block execute first then constructor execute (in inheritance case - parent class instance block then child class instance block)

Hope you guys understand the points i am trying to make, Let me know if you have any problem , Will be happy to help

-Thanks for Reading
-Noeik

Tuesday, January 10, 2017

Inheritance- Basics - Important Topic for Interview

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 
  • 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