• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Interfaces in Java

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,
Can anyone explain the importance of interfaces in Java.I know it enables multiple inheritance.But what is the big thing in inheriting a method name without implementation?Since we have to implement the method in our class what's the need in implementing the interface as we can do it without an interface?
Waiting for the reply....
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manoj.An interface defines the TYPE of an object.That is,in OO lingo,an interface provides the skeleton for an object definition.A class provides the IMPLEMENTATION of that definition as it feels so.
Hence,an interface does not need to have method implementation.It only lays down the rules (data types as well as the methods which will act on those data types) for an object.The method implementation is deferred to the class that implements that interface.There can be many different implementations for that object which depends on how actually the class programmer has overridden those methods.
Sometimes there arises a situation where a programmer may want to implement some common behaviour in a superclass.The subclass may use this implementation,or may decide to provide a new implementation.For this purpose,Java has given the provision of defining an ABSTRACT CLASS.
I hope i have been clear enough.
 
Manoj Chandran
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,
can anyone explain how various interface methods like getConnection() and createStatement() work without implementation in our jdbc programs?
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The jdbc driver provides the implementation classes which implement the various interfaces that you use in the java.sql package.
Regards,
Junaid
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manij,
It is a good question from u. More than all these, interface can be used for very good encapsulation. For example u have a class with 3 methods and u want 1 person to access all and other person to access only one method. In this case, u can create an interface and implement with only 1 method declaration in it.
Interface1 inter = new Class1(); // Class1 has 3 methods
inter object has only 1 method acces..
I think u understood what i mean?


------------------
SeE Consulting(P) Ltd
Bangalore,India
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manoj,
Multiple inheritance can be achieved more effectively by using aggregation. For example if class B inherits from A, but needs features of class C as well, then B can declare a variable of type C and use C's methods via the variable. Use of interfaces can come in handy in certain situations. Consider this
example.
<pre>
interface Shape{
public double getArea();
}
class Rectangle implements Shape{
private int length = 4;
private int width = 3;
public double getArea(){
return length * width;
}
// other methods related to Rectangles
}
class UnitSquare extends Rectangle{
public double getArea(){
return 1;
}
// other new methods and overriding methods
}
class Circle{
// methods related to Circles
}
class UnitCircle extends Circle implements Shape{
public double getArea(){
return Math.PI;
}
// other new methods and overriding methods
}
public class Test{
public void printArea(Shape sh)
{
System.out.println("Area of the shape is " + sh.getArea());
}
public static void main(String[] args)
{
Test t = new Test();
Rectangle r = new Rectangle();
UnitCircle uc = new UnitCircle();
UnitSquare us = new UnitSquare();
t.printArea(r);
t.printArea(us);
t.printArea(uc);
}
}
</pre>
The argument to the printArea method can be any class that belongs to the Rectangle hierarchy plus any other unrelated class that implements the Shape interface. This feature can be pretty useful for organising a class hierarchy. I hope this helps.
Rgds
Sahir
http://www.geocities.com/sahirshah/
[This message has been edited by Sahir Shah (edited January 27, 2001).]
 
Manoj Chandran
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai Sahir,
Thanks for your valuable suggestions.Hope to get a lot more in future..
Manoj
 
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic