File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interface & abstract class code question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "interface & abstract class code question" Watch "interface & abstract class code question" New topic
Author

interface & abstract class code question

Rafal Grzybacz
Greenhorn

Joined: Aug 19, 2006
Posts: 6
Given:

1. interface Animal {
2. void eat();
3. }
4.
5. // insert code here
6.
7. public class HouseCat extends Feline {
8. public void eat() { }
9. }

And five declarations:

1) abstract class Feline implements Animal { }
2) abstract class Feline implements Animal { void eat(); }
3) abstract class Feline implements Animal { public void eat(); }
4) abstract class Feline implements Animal { public void eat() { } }
5) abstract class Feline implements Animal { abstract public void eat(); }

Which declaration inserted independently at line 5, will compile?

The correct answer is 1,4 and 5. Can someone explain why 1,4 and 5 are correct and 2 and 3 are not? Thanks.
Stary Kapec
Ranch Hand

Joined: Dec 04, 2005
Posts: 81
Hi

2) abstract class Feline implements Animal { void eat(); }

Every method declared in interface is public abstract in default.
The overriding method cannot have a more restrictive access modifier than the method being overridden. So void eat() should be public.
There is also another probelm with eat(). It has no body but is not marked abstract.

3) abstract class Feline implements Animal { public void eat(); }

Now the method eat is public. That is ok. But it should also be marked abstract (it has no body) or has a body. For example public void eat() {}

1) abstract class Feline implements Animal { }

This is correct because abstract class does not have to provide implementations for any abstract method in Animal interface.
Abstract classes are the classes that does not have to be fully implemented.
Radoslaw Sztando
Ranch Hand

Joined: Mar 11, 2004
Posts: 40
1) abstract class Feline implements Animal { }
Feline is abstract class. Since it is not concrete it does not need to implement Animal methods.

2) abstract class Feline implements Animal { void eat(); }
eat() in Animal is public (like all methods in interfaces). That's why this won't compile.

3) abstract class Feline implements Animal { public void eat(); }
eat() is not marked abstract, so Feline should provide implementation. But there is not curly braces so there is no implementation. Compilation error.


4) abstract class Feline implements Animal { public void eat() { } }
correct implementation of interface (even if method eat() is not doing much )

5) abstract class Feline implements Animal { abstract public void eat(); }
perfectly ok. Abstract class with abstract method.
abstract public void eat(); is redundant since it is already declared in Animal interface.


Hope it helps.


Regards,
Radek Sztando
SivaRamiReddy Reddy
Greenhorn

Joined: Aug 14, 2006
Posts: 4
1)abstract class Feline implements Animal { }

1. interface Animal {
2. void eat();
3. }
4.
5. // insert code here
abstract class Feline implements Animal { }
6.
7. public class HouseCat extends Feline {
8. public void eat() { }
9. }


This ok.why means class Feline implements Animal so sub class must be provide the implementation of those methods which are specified in interface.but here we are not prvoding implementation of eat(), so Feline class will become abstract.

2) abstract class Feline implements Animal { void eat(); }

This is not correct.Why means..Feline implements Animal so we have to provide implementation at least empty implementation.

-- Atleat you write like this also..

abstract class Feline implements Animal { abstract public void eat(); }

-- But you dont write like this

abstract class Feline implements Animal { abstract void eat(); }

the reasons is :
You are attempting to assgin waker access priviliges means here access spcifier is default type,but super class Animal's is public so you cant override like this.as well as you cant use private and protected also.


3)abstract class Feline implements Animal { public void eat(); }

So here we sholud write abstract .Why means in interface public and abstract is default.(here overriding is coming)


4) abstract class Feline implements Animal { public void eat() { } }
5) abstract class Feline implements Animal { abstract public void eat(); }
Here 4 and 5 are correct
Rafal Grzybacz
Greenhorn

Joined: Aug 19, 2006
Posts: 6
Thank you everyone for your explanation to my question.
 
Consider Paul's rocket mass heater.
 
subject: interface & abstract class code question