JAVATECH NEWS

Java

CORE JAVA

Interview

OFF-CAMPUS

Thursday 25 December 2014

Inheritance in Java


1. What is inheritance?

Ans-Deriving new class(child class) from existing class(base class or parent class) such that new class acquire all the features of parent class/base class is called Inheritance. Java uses “extend” keyword for acquiring the features of parent class to child class.

public class Parent
{
String fathername="Chandan";
int fatherage=50;
}
class Child extends Parent
{
String name="Raju";
void display()
{
System.out.println("My name is "+name+" My fathername is "+ fathername);
System.out.println("My father's age is "+fatherage);
}
}
class Show
{
public static void main(String args[])
{
Child c=new Child();
c.display();
}
}

2. What are the types of inheritance.?


Ans:There are 5 types of inheritance.

1. Single Inheritance.

2. Multilevel Inheritance

3. Hierarchical Inheritance

4.Hybrid Inheritance.

5. Multiple Inheritance.

3.What is single inheritance?

Ans: If One class is extended by only one class,then it is called single inheritance.
Example of single inheritance
class One
{
void methodOne()
{
System.out.println("This is Parent class method");
}
}

class Two extends One
{
void methodTwo()
{
System.out.print("This is Child class method ");
}
public static void main(String args[])
{
Two t=new Two();
t. methodOne();
t.methodTwo();
}
}
O/P:
This is Parent class method
This is Child class method.

4.What is multi level inheritance?

Ans:If a class(here Two) inherit from a base class(here class One),and another class(here class Three) is derived from this derived class(class Two) then it is called Multi level inheritance.
Example of Multi level inheritance.
class One
{
void methodOne()
{
System.out.println("This is method of class One");
}
}

class Two extends One
{
void methodTwo()
{
System.out.println("This is method of class Two ");
}
}
class Three extends Two
{
void methodThree()
{
System.out.println("This is method of class Three");
}

public static void main(String args[])
{
Three t=new Three();
t. methodOne();
t.methodTwo();
t.methodThree();
}
}

O/P:
This is method of class One
This is method of class Two
This is method of class Three.

5.what is Hierarchical Inheritance?

Ans:if One class is extended by many classes.then it is called Hierarchical Inheritance.

6.what is Hybrid Inheritance ?

Ans: Hybrid inheritance is a combination of Single and Multipleinheritance.

7. What is Multiple inheritance and does java support multiple inheritance?

Ans) If a child class acquire the property from multiple classes then it is known as multiple inheritance. Java does not allow to extend multiple classes(multiple inheritance) To overcome this problem java introduces  Interfaces.

8. Why java doesn't support multiple Inheritance ?

Ans-Multiple inheritance leads to ambiguity and confusion in java so java does not support the  multiple Inheritance.
class One
{
void methodOne
{
System.out.print("In class One");
}
}

class Two
{
void methodOne
{
System.out.print("In class B");
}
}

class C extends A,B(if multiple inheritance supported)
//Now which method will be consider in class C from the above Two class A and B,so it leads to ambiguity and confusion. That’s why java doesn't support multiple Inheritance

9.Can a class extend itself.?


Ans-No.

10. How to implement multiple inheritance in java?


Ans:We can implement multiple inheritance through “interfaces” in java,In Java a class can not extend more than one classes, but a class can implement more than one interfaces.
Example:
interface Stallion // Adult Male horse
{
Float HT=6.8f;
void height();
}

interface Dam //Adult Female horse
{
Float HT=5.2f;
void height();
}

class Colt implements Stallion,Dam  // Colt Young male horse
{
public void height()
{
float ht=(Colt.HT+Filly.HT)/2;
System.out.print("Height of Colt is :"+ht);
}
}
class MultipleInheritance
{
public static void main(String args[])
{
Colt ch=new Colt ();
ch.height();
}
}
O/P: Height of Colt is :6.0

To know more about interface Please click here







No comments:

Post a Comment

Designed By Blogger Templates