3

Object-oriented questions about the interface, the abstract class and the concre...

 2 years ago
source link: https://www.codesd.com/item/object-oriented-questions-about-the-interface-the-abstract-class-and-the-concrete-class.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Object-oriented questions about the interface, the abstract class and the concrete class

advertisements

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();
    }
}

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

  1. How can I set Interface methods can only be used in derived classes?
  2. Why can't I set methods with protected access inside Interface?
  3. Why can't I set the accessor type of Class different than public after I implement the InterFace?
  4. 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 interfaceis 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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK