<< Chapter < Page Chapter >> Page >

(Although it isn't implicitly obvious, it is also possible to call any of the eleven methods defined in the Object class on an object's reference being stored as an interface type. Those eleven methods can be called on any object,including array objects, regardless of how the references are stored.)

Not the standard approach

If you are defining your own container, this is a satisfactory approach to implementation of the observer design pattern. However, you cannot use thisapproach when using containers from the standard collections framework, because those containers are designed to always store references as the generic type Object . In those cases, the casting solution of Question 7 (or the use of generics) is required.

Back to Question 8

Answer 7

C. A-intfcMethodX B-intfcMethodX

Explanation 7

The correct use of an interface

This program illustrates the correct use of an interface. It uses a cast of the interface type in the following fragment toresolve the problem that was discussed at length in Question 6 earlier.

void doIt(){ Object[]myArray = new Object[2];myArray[0] = new A();myArray[1] = new B();for(int i=0;i<myArray.length;i++){ ((X)myArray[i]).intfcMethodX(); }//end for loopSystem.out.println(""); }//end doIt()

Back to Question 7

Answer 6

A. Compiler Error

Explanation 6

What is a container?

The word container is often used in Java, with at least two different meaning. One meaning is to refer to the type of an object that is instantiatedfrom a subclass of the class named Container . In that case, the object can be considered to be of type Container , and typically appears in a graphical user interface (GUI) . That is not the usage of the word in the explanation of this program.

A more generic meaning

In this explanation, the word container has a more generic meaning. It is common to store a collection of object references in some sort of Javacontainer, such as an array object or a Vector object. In fact, there is a complete collections framework provided to facilitate that sort of thing ( Vector is one of the concrete classes in the Java Collections Framework) .

Storing references as type Object

It is also common to declare the type of references stored in the container to be of the class Object . Because Object is a completely generic type, this means that a reference to any object instantiated from any class (or any array object) can be stored in the container. The standard containers such as Vector and Hashtable take this approach.

(Note that this topic became a little more complicated with the release of generics in jdk version 1.5.)

A class named Base and an interface named X

In a manner similar to several previous programs, this program defines a class named Base and an interface named X as shown in the following fragment.

class Base{ public void inherMethod(){System.out.print("Base "); }//end inherMethod()}//end class Base interface X{public void intfcMethodX(); }//end X

Classes A and B extend Base and implement X

Also similar to previous programs, this program defines two classes named A and B . Each of these classes extends the class named Base and implements the interface named X , as shown in the next fragment.

Questions & Answers

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
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