Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Bailey Evans   on Apr 28 In Java Category.

  
Question Answered By: Abana Cohen   on Apr 28

...and there exactly seems to be some misunderstanding by the OP.
Have a look at the first line you've posted:

AppletContext ac = getAppletContext();

Have a sharp look: ac is defined as a _reference_ to some object
which belongs to some class; the only thing that is required from
this class  is that it implements the interface  AppletContext.
After this reference ac has been declared, ac is set to whatever the
method getAppletContext() returns; I suppose (I'm too lazy to check
the JavaDoc) that this method  creates some object which implements
the interface AppletContext, so this assignment is perfectly alright.

In other words: suppose you need to use some method xxx() which is
defined in an interface MyInterface. And suppose that you have one or
more classes Class1, Class2, Class3 which all implement this
interface MyInterface.
Then you have two different ways to declare something which allows
you to call for example  method Class3.xxx():

1)
Class3 c = new Class3();
c.xxx();

2) MyInterface c = new Class3();
c.xxx();

Obviously the first method is very direct and easy to understand. But
imagine that some day in the future you'll have to switch from Class3
to Class1 in this particular circumstance. Then you have to change
two things at once, namely by writing

Class1 c = new Class1();

Believe it or not, but there have been enough cases where the poor
chap who had to change this changed the "new Class1()" but forgot to
change the "Class3 c" to "Class1 c". What happened? Of course the
compiler refused to compile this, which means once more editing the
source code...

In the end it's more a matter of taste which method you prefer, but
both have their advantages and disadvantages. The biggest
disadvantage of the second approach you already discovered: it's easy
to puzzle newbies this way; this happened to me as well. :-)

Hope this helps.

Share: 

 

This Question has 1 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on How can an Interface like AppletContext show a document ? Or get search suggestion and latest updates.


Tagged: