Understanding Packages and Interfaces in Java
page 8 of 11
by Debjani Mallick
Average Rating: 
Views (Total / Last 10 Days): 59022/ 75


Objects, through the methods they expose, define their interaction with the outside world. An interface is a collection of instance variables and instance methods without having any body, i.e., the methods do not have any definition. Once an interface is declared, any number of classes can implement that method. In other words, the classes contain all the members of the interface and they implement the definition of the method declared in the interface. By implementing an interface, we can achieve multiple and hierarchical inheritance as well as achieve dynamic (run time) polymorphism.

Declaring an Interface

The syntax for declaring an interface is:

Listing 8

Accessspecifier interface interfacename
specifier datatype variable = value;
      specifier returntype methodname(parameter);

In the above example interface is a keyword.

Access specifiers – This must be public or it can be omitted as well. If the access specified is omitted, then the classes of the same package can only implement that interface in which the interface is present and if the access specified is public then classes of any package can implement the interface.

Implementing an Interface

The classes which implement an interface contain all the members of the interface as well as some of its own member. The syntax of implementing an interface is a shown below.

Listing 9

class classname implements interfacename
      own members;
      public void display()

Example of implementing Interface

The example below shows the implementation of an interface D2 having a function area.

Listing 10

interface D2
      public void area(int x,int y);
class triangle implements D2
      int base;
      int height;
      public void area(int x,int y)
            int a;
            base = x;
            height = y;
            a = ½*base*height;
            System.out.println(“Area is:”+a);
class rectangle implements D2
      int length;
      int breadth;
      public void area(int x,int y)
            int a;
            length = x;
            breadth = y;
            a = length*breadth;
            System.out.println(“Area is:”+a);
class result
public static void main(String a[ ])
      D2 d;
triangle t = new triangle();
      d = t;
      rectangle r = new rectangle();
      d = r;

Here the function area () of interface D2 is first implemented by the class triangle to calculate the area of a triangle and then it is implemented by another class named rectangle for calculating the area of a rectangle.

Inheritance in Interface

Look at the code sample given below.

Listing 11

interface a
      public void x();
      public void y();
interface b extends a
      public void z();
class c implements a
      methods of a to be declared
class c implements b
      methods of a and b are to be declared


An interface can be derived from another interface. The syntax of inheriting an interface is the same as inheriting a class. When a class implements an interface, which is again inherited form another interface, in such cases the class must define all the methods of the base interface as well as the derived interface.

View Entire Article

User Comments

Title: air max shoes r Five, please,��   
Name: anciag
Date: 2012-12-05 2:43:16 AM
r Five, please,�� said the loud-speaker
Number Five was Roger Clint��s mount with the long white stockings. ��Do you [url=http://www.airmax2013cheap.com/nike-air-max-2011-c-5.html]air max 2011[/url] know what he calls it?�� Bee said. ��Operation Stockings.��
��It��s very ugly,�� Brat said. ��Looks as if he had walked through [url=http://www.airmax2013cheap.com/]nike new air max shoes[/url] air max 2013 a trough of whitewash.��
��He can jump, though.��
He could certainly jump, but he had phobia about water.
��Poor Roger,�� laughed Bee, watching Stockings refuse the water. ��He has been jumping him backwards and forwards across the duck pond at home in the hope of curing him, and now he does this!��
Stockings continued to refuse, and Clint had to take him out, in [url=http://www.airmax2013cheap.com/]new air max shoes[/url] a burst of sympathetic applause.
Numbers Six and Seven had one fault each.
Number Eight was Simon on Timber.
The black horse came into the ring exactly as he had come out of his box on the day Brat first saw him, pleased with himself and ready fo
Title: wholesale cheap ravens jerseys   
Date: 2012-06-03 10:53:21 PM
Title: 2012 NFL jerseys   
Name: NIKE NFL jerseys
Date: 2012-05-20 11:28:31 PM
[/pre]Cheap NFL,NBA,MLB,NHL
[url=http://www.jersey2shop.com/]Jerseys From China[/url]
[url=http://www.jersey2shop.com/]2012 nike nfl Jerseys[/url]
[url=http://www.jersey2shop.com/]cheap China Jerseys[/url]
[url=http://www.jersey2shop.com/]Sports Jerseys China[/url]
[url=http://www.jersey2shop.com/NFL-Jerseys-c68/]NFL Jerseys China[/url]
[url=http://www.jersey2shop.com/NBA-Jerseys-c77/]NBA Jerseys China[/url]
NHL Jerseys China
[url=http://www.jersey2shop.com/MLB-Jerseys-c94/]MLB Jerseys China[/url]NFL jerseys For Sale online.All Our Jerseys Are Sewn On and Directly From Chinese Jerseys Factory
[pre]We Are Professional China jerseys Wholesaler
[url=http://www.cheapjersey2store.com/]Wholesale cheap jerseys[/url]Cheap mlb jerseys
[url= http://www.cheapjersey2store.com/]2012 mlb all atar jerseys[/url]
[url= http://www.cheapjersey2store.com/ [/url]Cheap China Wholesael[/url]
[url= http://www.cheapjersey2store.com/]Wholesale jerseys From China[/url]
[url=http://www.cheapjersey2store.com/]2012 nike nfl Jerseys[/url]Free Shipping,Cheap Price,7 Days Deliver
We are professional jerseys manufacturer from china,wholesal
sports [url= http://www.cheapjersey2store.com/]Jerseys From China[/url]
[url=http://www.cheapjersey2store.com/NFL-Jerseys-c68]NFL jerseys China[/url]
[url=http://www.cheapjersey2store.com/NHL-Jerseys-c96/]NHL Jerseys China[/url]
[url=http://www.cheapjersey2store.com/NBA-Jerseys-c77/]NBA Jerseys China[/url]
[url=http://www.cheapjersey2store.com/MLB-Jerseys-c94/]MLB Jerseys China[/url]
[url= http://www.cheapjersey2store.com/]China Jerseys[/url],Free Shipping
We are professional jerseys manufacturer from china,wholesal
sports [url= http://www.jerseycaptain.com/]cheap jerseys sale online [/url]
[url= http://www.jerseycaptain.com/]2012 nike nfl Jerseys[/url]
[url=http://www.jerseycaptain.com/NFL-Jerseys-c68]cheap NFL jerseys China[/url]
[url=http://www.jerseycaptain.com/NHL-Jerseys-c96/]NHL Jerseys C
Title: Real time example   
Name: Axe
Date: 2012-04-10 10:19:44 PM
Any real time example for package which is used in daily life
Title: creating a package   
Name: golnaz
Date: 2012-01-23 4:01:45 AM
Thanks a lot, was very useful
Title: Packages and interfaces   
Name: Lesedi
Date: 2011-02-25 5:55:35 AM
post more details
Title: packages and interfaces in java   
Name: sinha timir krishna murari
Date: 2010-11-14 6:59:20 AM
explanation of the concept in full detail with the definition of the topic
Title: drawback of interface   
Name: neeraj singh
Date: 2010-09-16 4:17:34 PM
post in detail moreeeeeeeeeeee
Title: creating package   
Name: Nitesh
Date: 2010-04-20 12:53:18 AM
gives alot of confidence to person
Title: thakur   
Name: Nitesh
Date: 2010-04-20 12:49:47 AM
give more information
Title: package   
Name: puja
Date: 2010-01-27 11:05:13 AM
can give more information about advantages of packages.
Title: Mistake   
Name: Gyandeep
Date: 2009-11-19 5:16:23 AM
Ma'am, you have made a slight mistage here. The refernce variable you have used d takes r to its account and prints r.area(). It should be d.area. In the main().

Thanks and cheers!
Title: Interfaces and packages   
Name: Tamkeen
Date: 2009-07-07 2:55:10 AM
Very precise and easily understandable language.
Title: package   
Name: Duraipandi
Date: 2008-10-14 7:34:39 AM
It is very nice.here syntax and examples are each and every thing fine.
Title: very nice elaboration   
Name: Regalla Naresh Reddy
Date: 2008-06-10 3:36:05 AM
Its vey nice.If you include syntax and an exmaple on syntax looks very nice.So try it
Title: doubt   
Name: raghavendra
Date: 2008-03-15 3:01:11 AM
i have created a package named account and i created a new folder name test inside test i written a java program to access the package account.. its saying package account not found.. kindly help me.. if any..
Title: very nice   
Name: antony
Date: 2008-01-22 6:14:33 AM
its very very useful for beginers.so thanks for you.and also i want more in swing and awt etc
Title: Nice   
Name: Ed
Date: 2007-08-12 10:34:37 PM
Title: Good work   
Name: Rick
Date: 2007-08-10 10:50:14 PM
It is a good piece of work.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-06-18 6:12:33 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search