• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

interface & abstract class code question

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you everyone for your explanation to my question.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic