• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

interface and classes

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..
I have this interface 'abc' that i'm implementing in 'fac' class. My 'abc' interface is as below
public interface abc
{
public void displayDetails();
public void setOccupied(boolean state);
public boolean isOccupied();
}
and my class 'Fac'
public abstract class fac implements abc
{
abc [] aa = new abc[5];
public fac() {
}
}
now my quetsion is that how can i create array of abc object ???
if i try
aa[0] = new fac(100);
it gives the error ----------------- "]expected"
i want to create the objects as such
aa[0] = new fac(500);
aa[1] = new rest(1200);
and so on...
(rest is another class that implements the abc interface)
2)if i don't declare the fac class as an abstract then the error that i get is
"class fac should be declared as abstract ,it doesnot define the method isOccupied in interface abc"...
why am i getting this error .. what should i do so that this error goes away ..i;m not supposed to declare the fac class as abstract..!
i'm new to this thing ... so plz help me out on this...
any ideas..
thanxs..
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) you can't instantiate Fac because it's abstract so the compiler thinks you're creating an array of them.
2) implement that method and your other problem should go away as well.
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



and my class 'Fac'


Hi Rekha,
The idea behind interfaces is that you are declaring (abstract) methods that classes which implement the interface must provide concrete implementations for.
Your abc inteface has 3 methods which fac (and rest, etc.) must implement -
displayDetails()
setOccupied(boolean state)
isOccupied().
If your class does not need to use these implementations they could provide concrete implementations of these methods which do nothing. I.E. have no code between { and }.
E.g.

2)if i don't declare the fac class as an abstract then the error that i get is
"class fac should be declared as abstract ,it doesnot define the method isOccupied in interface abc"...
why am i getting this error


The only way for fac to implement the abc interface and not implement the 3 methods defined in abc is if fac is abstract. The java compiler then expects that a non-abstract sub-class of fac will implement these methods.

what should i do so that this error goes away ..


Add the methods to your classes as described above.
HTH,
Fintan
[ April 14, 2004: Message edited by: Fintan Conway ]
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel you have mixed up the terms interface, abstract class and non-abstract class.
Interface defines the contract between 2 objects such that they can communicate with. If class A implements interface IA, and B wanna invoke a method in class A, B can look at the interface IA to see what methods/functions are provided by class A, and then call the corresponding methods.
You may find Interface is not much helpful, if all methods in class A is public, since B can see all methods in A without IA. However, A may not wanna to disclose all methods to others (like private and protected methods), and thus, interface allows A to define the methods that are going to be disclosed.
Abstract class, similar to Interface, usually define the skeleton of its subclasses. For example, ANIMAL is an abstract class. An animal should be able to eat and walk. However, different animals use different ways to eat and walk. Say, dogs uses months only, but human may use folk and spoon. Thus, Abstract class defines what MUST be able to do by an animal. If any subclass wanna be an animal, like human, birds, dogs, etc, they MUST define their own way for eat and walk!
Hope this clear.
Nick
 
Rekha Pande
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanxs all for your replies ... i have tried what was mentioned and now my fac class in non-abstract ... but there is one problem....
when i try to create an array of 'abc' objects it gives me the error
'] expected'
i'm creating the array like this:
abc obs[] = new abc[5] ;
obs[0] = new fac(500);
What i want to do is that i want to display that
if obs[0].setOccupied(true)..then the program should return occupied else not occupied.
But why is it giving me the error
'] expected' for this line ---- obs[0] = new fac(500);
Is there something wrong with the way i'm writting the code..?
???
Thanxs all..
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Where did you put that code?
If your code is:

Then this does NOT work!
You need to be this:

Does this your case?
Nick
 
Rekha Pande
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you .... yeh that was the mistake that i was making...!!!
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rekha,
That's great. Hope you can fix the problems.
Nick
 
Run away! Run away! Here, take this tiny ad with you:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic