Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define an abstract class that inherits an infterface, but does not implement it

incorporating leppies feedback it compiles - but IMO some drawbacks I want each sub class to be forced by the compiler to define their own Uri property. Code as it is now:

[<AbstractClass>] 
type UriUserControl() = 
    inherit UserControl()
    interface IUriProvider with 
        member this.Uri with get() = null

Interesting enough, the class I defines which inerits from above does not show a public Uri property:

type Page2() as this =
    inherit UriUserControl()
    let uriStr = "/FSSilverlightApp;component/Page2.xaml"
    let mutable uri = new System.Uri(uriStr, System.UriKind.Relative)
    do
        Application.LoadComponent(this, uri)

    member public this.Uri with get () = uri

I would like to define an abstract class that inherits from UserControl and my own Interface IUriProvider, but doesn't implement it. The goal is to be able to define pages (for silverlight) that implement UserControl but also provide their own Uri's (and then stick them in a list / array and deal with them as a set:

type IUriProvider = 
interface
    abstract member uriString: String ;
    abstract member Uri : unit -> System.Uri ;
end

[<AbstractClass>] 
type UriUserControl() as this = 
    inherit IUriProvider with
        abstract member uriString: String ;
    inherit UserControl()

Also the Uri in the definition - I would like to implement as a property getter - and am having issues with that as well.

this does not compile

type IUriProvider = 
    interface
        abstract member uriString: String with get;
end
like image 463
akaphenom Avatar asked May 22 '10 19:05

akaphenom


People also ask

When a class inherits an abstract class and it does not implement?

3. If a class inheriting an abstract class does not define all of its function then it will be known as? Explanation: Any subclass of an abstract class must either implement all of the abstract method in the superclass or be itself declared abstract. 4.

Can an abstract class inherit an interface?

An abstract class can inherit a class and multiple interfaces.

Does an abstract class have to implement an interface?

Java Abstract class can implement interfaces without even providing the implementation of interface methods. Java Abstract class is used to provide common method implementation to all the subclasses or to provide default implementation.

What happens if I will not provide an abstract method in abstract class and interface?

And yes, you can declare abstract class without defining an abstract method in it. Once you declare a class abstract it indicates that the class is incomplete and, you cannot instantiate it. Hence, if you want to prevent instantiation of a class directly you can declare it abstract.


2 Answers

Here is a way to do it:

type IUriProvider =  
    abstract member UriString: string
    abstract member Uri : System.Uri

[<AbstractClass>]  
type UriUserControl() as this =  
    inherit System.Windows.Controls.UserControl() 
    abstract member Uri : System.Uri
    abstract member UriString : string
    interface IUriProvider with 
        member x.Uri = this.Uri
        member x.UriString = this.UriString

Note that you have to provide an implementation of the interface (since all interface implementations in F# are explicit), but this can just refer back to abstract members in the class. Then you can subclass thusly:

type ConcreteUriUserControl() =
    inherit UriUserControl()
    override this.Uri = null
    override this.UriString = "foo"
like image 197
Brian Avatar answered Nov 25 '22 23:11

Brian


From a .NET point of view, you would need to at least provide an abstract implementation for the interface. But that again could proof problematic due to default interface accessibility, which would require some more glue again for an explicit implementation.

like image 29
leppie Avatar answered Nov 25 '22 23:11

leppie