<< Chapter < Page Chapter >> Page >

Interfaces are critical to Java programming

I will suggest that there is little if anything useful that can be done in Java without understanding and using interfaces.

In support of this suggestion, I will discuss several real-world examples of the use of the Java interface, including the Delegation Event Model, the Model View Control paradigm, and iterators in Java data structures.

Discussion and sample code

Listing 6 near the end of the module contains a very simple program named Poly05 .

The purpose of this program is to illustrate polymorphic behavior using interfaces in addition to class inheritance.

Designed to illustrate structure

This is a skeleton program designed solely to illustrate the inheritance and interface implementation structure in as simple a program as possible. (I will put some meat on this skeleton using another program in the next module.)

Empty methods

Except for the two methods that return type String , all of the methods in the program are empty. (Methods that return type String cannot be empty. They must contain a return statement in order to compile successfully.)

Interface definitions

Listing 1 shows the definition of two simple interfaces named I1 and I2 .

Listing 1 . Definition of interfaces named I1 and I2.
interface I1{ public void p();}//end interface I1 //===================================//interface I2 extends I1{ public void q();}//end interface I2

Similar but different

An interface definition is similar to a class definition. However, there are some very important differences.

No single hierarchy

To begin with, unlike the case with classes, there is no single interface hierarchy. Also, multiple inheritance is allowed when extending interfaces.

A new interface can extend none, one, or more existing interfaces. In Listing 1 , I2 extends I1 , but I1 doesn't extend any other interface (and unlike classes, an interface doesn't automatically extend another interface by default) .

Two kinds of members allowed

Only two kinds of members are allowed in an interface definition:

  • Methods, which are implicitly abstract
  • Variables, which are implicitly constant (final)

Each of the interfaces in Listing 1 declares an implicitly abstract method (an abstract method does not have a body) .

Neither of the interfaces in Listing 1 declares any variables (they aren't needed for the purpose of this module) .

A new data type

I told you earlier that when you define a new class, you cause a new data type to become available to your program. The same is true of an interface definition. Each interface definition constitutes a new type.

The class named A

Listing 2 defines a very simple class named A , which in turn defines two methods named toString and x .

Listing 2 . Definition of the class named A.
class A extends Object{ public String toString(){return "toString in A"; }//end toString()//---------------------------------//public String x(){ return "x in A";}//end x() //---------------------------------//}//end class A

Overridden toString

The method named toString in Listing 2 is actually an overridden version of the method having the same name that is defined in the class named Object . (Recall that a previous module made heavy use of overridden versions of the toString method.)

Questions & Answers

Biology is a branch of Natural science which deals/About living Organism.
Ahmedin Reply
what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
cell is the smallest unit of the humanity biologically
Abraham
what is biology
Victoria Reply
what is biology
Abraham
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask