<< Chapter < Page Chapter >> Page >

In this case, the only subclass-superclass relationship between the classes named A and B was that they were both subclasses of the same superclass. Even that relationship was established forconvenience, and was not a requirement.

Different behavior of interface methods

The methods having the same signature, (declared in the common interface, and defined in the classes) , need not have any similarity in terms of behavior.

A new interface relationship

The fact that both classes implemented the interface named X created a new relationship among the classes, which is not based onclass inheritance.

Back to Question 5

Answer 4

C. Base A-intfcMethod

Explanation 4

Illustrates the use of an interface as a type

The program defines a class named Base , and a class named A , which extends Base , and implements an interface named X , as shown below.

class Base{ public void inherMethod(){System.out.print("Base "); }//end inherMethod()}//end class Base class A extends Base implements X{public void inherMethod(){ System.out.print(" A-inherMethod "); }//end inherMethod()public void intfcMethod(){System.out.print("A-intfcMethod "); }//end intfcMethod()}//end class A interface X{public void intfcMethod(); }//end X

Implementing interfaces

A class may implement none, one, or more interfaces.

The cardinal rule on interfaces

If a class implements one or more interfaces, that class must either be declared abstract, or it must provide concrete definitions of all methodsdeclared in and inherited into all of the interfaces that it implements. If the class is declared abstract, its subclasses must provide concrete definitions ofthe interface methods.

A concrete definition of an interface method

The interface named X in this program declares a method named intfcMethod . The class named A provides a concrete definition of that method.

(The minimum requirement for a concrete definition is a method that matches the method signature and has an empty body.)

Storing object's reference as an interface type

The interesting part of the program is shown in the following code fragment.

void doIt(){ Base myVar1 = new Base();myVar1.inherMethod(); X myVar2 = new A();myVar2.intfcMethod(); System.out.println("");}//end doIt()

The above fragment instantiates a new object of the class named A , and saves a reference to that object in a reference variable of the declared type X .

How many ways can you save an object's reference?

Recall that a reference to an object can be held by a reference variable whose type matches any of the following:

  • The class from which the object was instantiated.
  • Any superclass of the class from which the object was instantiated.
  • Any interface implemented by the class from which the object was instantiated.
  • Any interface implemented by any superclass of the class from which the object was instantiated.
  • Any superinterface of the interfaces mentioned above.

Save object's reference as implemented interface type

In this program, the type of the reference variable matches the interface named X , which is implemented by the class named A .

Questions & Answers

How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
How can I make nanorobot?
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
how can I make nanorobot?
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?