Object Oriented questiond about interface, abstract class and concrete class
1) OnCreate is public method of instantiated object from ClsLast class. But I wanted to restrict OnCreate method as protected.
interface InterFace {
void OnCreate();
}
class ClsFirst implements InterFace {
@Override
public void OnCreate() { }
}
class ClsLast extends ClsFirst{ }
class Test {
static void main(){
ClsLast objClsAct = new ClsLast();
objClsAct.OnCreate();
}
}
开发者_StackOverflow中文版
2) If I set OnCreate method as protected in InterFace
interface InterFace {
protected void OnCreate();
}
I'm getting error like this:
Illegal modifier for the interface method OnCreate; only public & abstract are permitted
3) If I set protected the method inside the ClsFirst class which implements InterFace like this:
interface InterFace {
void OnCreate();
}
class ClsFirst implements InterFace {
@Override
protected void OnCreate() { }
}
I'm getting this error:
Cannot reduce the visibility of the inherited method from InterFace
4) When I change the ClsFirst as abstract class and implements InterFace
interface InterFace {
void OnCreate();
}
abstract class ClsFirst implements InterFace {
}
I haven't to implement OnCreate method inside the ClsFirst class but ClsLast why?
Summary
- How can I set Interface methods can only be used in derived classes?
- Why can't I set methods with protected access inside Interface?
- Why can't I set the accessor type of Class different than public after I implement the InterFace?
- abstract classes that even if they implements an interface don't have to add unimplemented methods itself until one class derives abstracted classes. Why?
Thank you so much for your kind answers from now.
An interface
is per definition a public contract
. By implementing an interface you promise to provide a set of methods (and properties in other languages).
An interface has nothing in common with abstract
classes because you can implement as many interfaces as you want on a single class and you implement
instead of derive
from it.
An abstract class
is a base class, its like a partially functionally base which is used to share implementation details between different classes and provides a contract, too. However it's more an internal contract for all derived classes. Most times abstract classes are used to force all derived classes into a common pattern instead of sharing a public contract.
An abstract class can force a derived class to implement a method (or provide a suitable default implementation using the virtual
keyword) and take use of this method (this is called Template Pattern).
If you implement an interface on an abstract class you don't have to provide all interface methods because the abstract class can not be instantiated. However if you don't implement all interface methods the first non abstract class in your hierarchy has to provide the missing methods.
Interface methods are always public. You can not reduce the scope of these methods. Use a protected abstract method instead in an abstract class.
You cannot do it. You can set methods both as virtual or abstract if you have an middle class which implements only a part of defined in interface contract.
You also cannot do it and the reason is simple, interface defines a contract, a set of operations which implementation is requested to have. You can implement the interface explicitly in your abstract class or restrict whole interface to be "internal", at least in c# in case if you would like to keep it 'in-house'.
You cannot do it because you promised to implement an interface.
Because another thing which you indirectly pointed once you mark class as an abstract is that you cannot instantiate it. Therefore you pass the requirement of implementing interface to the first class which you can create the instance of (I mean the chain of inheritance).
ad1. (c# example1)
interface IDummyInterface
{
void OnCreate();
void Process();
void OnFinish();
}
abstract class DummyAbstract : IDummyInterface
{
public virtual void OnCreate()
{
}
public abstract void Process();
public void OnFinish()
{
OnFinishInternal();
}
protected abstract void OnFinishInternal();
}
class DummyImplementor : DummyAbstract
{
public override void OnCreate()
{
// some other action here
base.OnCreate();
}
public override void Process()
{
}
protected override void OnFinishInternal()
{
}
}
Actually you can hide the method, until the your concrete class is casted to an "Interface" instance by implementing the Interface explicitly:
abstract class ClsFirst : Interface
{
void Interface.OnCreate()
{
this.OnCreate();
}
protected abstract void OnCreate();
}
ClsFirst last = new ClsLast();
last.OnCreate(); // Compiler Error, since OnCreate() is protected
Interface lastAsInterface = last;
lastAsInterface.OnCreate(); // Works, and calls the abstract OnCreate() method.
1) Why would you want to implement a protected interface
to begin with? It does not add any value. You already have a way to make sure all derived classes comply with a given contract:
protected abstract void OnCreate();
2) Interfaces define a public
contract. By public
I mean that it has the same accesibility as the interface itselft. That is why accesibility modifiers are not permitted when defining interface
members.
3) You can not decrease accesibility of any virtual member or interface members as you would be breaking the interface's contract. In your example ((IOnCreate)ClsFirst).OnCreate()
would not be possible as OnCreate()
is not accesible, which would mean that ClsFirst
does not implement IOnCreate
interface.
精彩评论