Total Pageviews

Saturday, September 22, 2012

CAN TWO CLASSES HAVE A FUNCTION WITH SAME SIGNATURE WITHOUT INTERFACE?

Consider the program...


class A
{
    void print()
    {
        System.out.println("hai in class A");
    }
}

class B
{
    void print()
    {
        System.out.println("hai in class B");
    }
}

public class y_interface
{
     public static void main(String arg[])
     {
         A obj=new A();
         obj.print();
         B obj1=new B();
         obj1.print();
         
     }
}

Here class A and class B have a function void print() with same signature. The above program runs without any error. Its output is

OUTPUT:
hai in class A
hai in class B

The main intention of this blog is that, there may be confusion like it is not possible to have two classes having function with same signature without interface. The main goal of introducing interface concept in Java is to provide support for multiple inheritance and for polymorphism(i.e. ability to perform same operation on a variety of objects). This blog clears this particular doubt.

Thanks for reading :-) Comment about the blog. Queries are highly welcomed. Stay tuned for my next blog ;-)

No comments:

Post a comment