Sunday, 14 April 2013

Interview Question #8 What happens when a class implements two interfaces with same method?

This is a very interesting question. If you still don't get it let me give an example to explain it further -
We have two interfaces  - InterfaceA and InterfaceB. Then we have a concrete class which implements these interfaces.

Code of InterfaceA

package testCodes;

public interface InterfaceA {
    public void color();

Code for InterfaceB

package testCodes;

public interface InterfaceB {
    public void color();

Code for class ConcreteClass

package testCodes;

public class ConcreteClass implements InterfaceA, InterfaceB {
    public void color() {

Note that though we have same method in both interface we need to implement it only once in our concrete subclass. So it does not matter if we have two interfaces with same method. It will be implemented only once.

Problem arises when.....

   Problem will arise when there are two interfaces having method with same name and parameter but different return type. When a class will implement these two interface it will have to implement these two methods with same name and parameters but different return type. But this is not allowed. JVM will not allow this code to be compiled. Functions can have same name(function overloading) but they must have different signatures(different parameters.)

Inheritance in Java

When we said Java is an Object oriented programming language we mentioned some of it's features like Inheritance, polymorphism, Data Encapsulation etc.In this tutorial we will see what is inheritance, why do we need it and how do we use it in Java.

Why do we need inheritance?

Lets say you want to make different classes like Dog, Cat, Lion, Tiger etc. Each class will have some common attributes like food,picture,location etc. and some common methods like eat(), makeNoise() etc.
To avoid duplicate code we create a common class with common code(variables and functions) and then make other requires classes as subclasses of animal class.

So we need inheritance to avoid duplication of code and make our program more sensible and understandable.

Understanding inheritance in Java

   It is very simple to use inheritance in Java. Keyword that we use in this context is extends. A subclass extends a super class.Before looking at how to actually use it it is very important to decide when can we use inheritance? There is a simple 

IS-A Test : When ever you want to decide if inheritance is applicable, You can always use this test. So in above case we say Dog is an Animal, Tiger is an animal.. and so on.

 Let us see now how do we actually use inheritance in Java..

First we write class  Animal which is the super class.

package animalsData;

public class Animal {

    public String food;
    public String noise;

    public void run() {

Then we write the subclass Dog

package animalsData;

public class Dog extends Animal {
    public void eat()
        System.out.println("Eating bone \n");

and finally the Coordinator class to run our code.

package animalsData;

public class Coordinator {
    public static void main(String[] args) {
        Dog dog = new Dog();;;
output : Eating bone

eat() is a function  of class Dog whereas run() is a function of class Animal which is inherited by class Dog.
We can inherit any such class by using extends keyword. Note that Java does not support multiple inheritance. This means that a single subclass cannot inherit from multiple subclasses. To avoid duplicate data in Java we can also use interfaces which we will see in our next tutorial.

Remember to apply IS-A test to indentify inheritance.

If Class 'X' extends Class 'Y' then Class 'X' IS-A 'Y'. This holds true in the entire inheritance tree. So if 'X' extends 'Y' and 'Z' extends X then 'Z' IS-A 'Y'.

Why multiple inheritance is not supported in Java?

  Java does not support multiple inheritance for the sake of simplicity for which it is built. Consider the Diamond problem of multiple inheritance - 

We have two classes B and C inheriting from A. Assume that B and C are overriding an inherited method and they provide their own implementation. Now D inherits from both B and C doing multiple inheritance. D should inherit that overridden method, which overridden method will be used? Will it be from B or C? Here we have an ambiguity.

Java provides means to tackle this and that is interfaces.
We will see interfaces in our next tutorial. 

t> UA-39527780-1 back to top