Chapter 8
Abstract Class
Abstract Class/Interface
• In object-oriented programming, an abstract class is used as a
base class of a hierarchy, and represents common
functionality of a diverse set of object types
• It is defined as same way as class
• Abstract classes are classes that leave some or all members
unimplemented, so that implementations can be provided by
derived classes.
• Abstract classes are those which don't provide full
implementation of class members
Abstract Class/Interface
Abstract Class
used as a base class of a hierarchy and represents
common functionality
can have implemented and unimplemented
members.
A class that inherits abstract class must provide
implementation of all abstract methods & Class
Abstract classes are used to achieve abstraction.
We cannot create an instance of an abstract class
because the class is not fully implemented.
Syntax
[<AbstractClass>]
type [ accessibility-modifier ] abstract-class-name =
[ inherit base-class-or-interface-name ]
[ abstract-member-declarations-and-member-
definitions ]
// Abstract member syntax.
abstract member member-name : type-signature
Example:1
Person
member x.Name
abstract Greet ()
Student
member x.StudentID
override x.Greet()
Teacher
member x.Expertise
override x.Greet()
[<AbstractClass>]
type Person(name) =
member x.Name = name
abstract Greet : unit -> unit
type Student(name, studentID : int) =
inherit Person(name)
member x.StudentID = studentID
override x.Greet() = printfn "Student %s" x.Name
type Teacher(name, expertise : string) =
inherit Person(name)
member x.Expertise = expertise
override x.Greet() = printfn "Teacher %s." x.Name
let st = new Student("Amit", 1234)
let tr = new Teacher("Apurva", "Java")
//Overriden Greet
st.Greet()
tr.Greet()
[<AbstractClass>]
type AbstractClass1() =
class
abstract member ShowClassName : unit -> unit
end
type DerivedClass1() =
class
inherit AbstractClass1()
override this.ShowClassName() = printf "This is derived class."
end
let a = new DerivedClass1()
a.ShowClassName()
Interface
• It provides pure abstraction.
• Abstraction is about hiding unwanted details while showing
most essential information.
• It is a collection of abstract methods.
• A Class which implements interface must provide definition
for its all methods.
Interface
Interface Class
In an interface declaration the members are not
implemented.
The members are abstract, declared by
the abstract keyword.
We may provide a default implementation using
the default keyword.
We can implement interfaces either by using object
expressions or by using class types.
In class or object implementation, We need to
provide method bodies for abstract methods of the
interface..
Interface :Syntax
[ attributes ]
type interface-name =
[ interface ] [ inherit base-interface-name ...]
abstract member1 : [ argument-types1 -> ] return-type1
abstract member2 : [ argument-types2 -> ] return-type2
...
[ end ]
The keywords interface and end, which mark
the start and end of the definition, are optional
Interface :Example
type Person =
abstract Name : string
abstract Enter : unit -> unit
abstract Leave : unit -> unit
Calling Interface Methods
• Interface methods are called through the interface,
• not through the instance of the class or type
implementing interface.
• To call an interface method, you up cast to the
interface type by using the :> operator (upcast
operator).
Example :
(s :> Person).Enter()
(s :> Person).Leave()
Example:1
Person
abstract Name
abstract Enter ()
abstract Leave()
Student
member this.ID
member this.Name
member this.Enter()
member this.Leave()
Teacher
member this.ID
member this.Name
member this.Enter()
member this.Leave()
Implements
type Person =
abstract Name : string
abstract Enter : unit -> unit
abstract Leave : unit -> unit
type Student(name : string, id : int) =
member this.ID = id
interface Person with
member this.Name = name
member this.Enter() = printfn "Student entering premises!"
member this.Leave() = printfn "Student leaving premises!"
type Teacher(name : string, id : int) =
member this.ID = id
interface Person with
member this.Name = name
member this.Enter() = printfn "Teacher entering premises!“
member this.Leave() = printfn "Teacher leaving premises!“
let s = new Student("Amit", 1234)
let t = new Teacher("Rohit", 34)
(s :> Person).Enter()
(s :> Person).Leave()
(t :> Person).Enter()
(t :> Person).Leave()
Interface Inheritance
Interfaces can inherit from one or more base interfaces.
Interface1
abstract member doubleIt
Interface2
abstract member tripleIt
Interface3
inherit Interface1
inherit Interface2
abstract member printIt()
multiplierClass
member this.doubleIt(a)
member this.tripleIt(a)
member this.printIt(a)
Implements
type Interface2 =
abstract member tripleIt: int -> int
type Interface3 =
inherit Interface1
inherit Interface2
abstract member printIt: int -> string
type multiplierClass() =
interface Interface3 with
member this.doubleIt(a) = 2 * a
member this.tripleIt(a) = 3 * a
member this.printIt(a) = a.ToString()
let ml = multiplierClass()
printfn "%d" ((ml:>Interface3).doubleIt(5))
printfn "%d" ((ml:>Interface3).tripleIt(5))
printfn "%s" ((ml:>Interface3).printIt(5))

.NET F# Abstract class interface

  • 1.
  • 2.
    Abstract Class/Interface • Inobject-oriented programming, an abstract class is used as a base class of a hierarchy, and represents common functionality of a diverse set of object types • It is defined as same way as class • Abstract classes are classes that leave some or all members unimplemented, so that implementations can be provided by derived classes. • Abstract classes are those which don't provide full implementation of class members
  • 3.
  • 4.
    Abstract Class used asa base class of a hierarchy and represents common functionality can have implemented and unimplemented members. A class that inherits abstract class must provide implementation of all abstract methods & Class Abstract classes are used to achieve abstraction. We cannot create an instance of an abstract class because the class is not fully implemented.
  • 5.
    Syntax [<AbstractClass>] type [ accessibility-modifier] abstract-class-name = [ inherit base-class-or-interface-name ] [ abstract-member-declarations-and-member- definitions ] // Abstract member syntax. abstract member member-name : type-signature
  • 6.
    Example:1 Person member x.Name abstract Greet() Student member x.StudentID override x.Greet() Teacher member x.Expertise override x.Greet()
  • 7.
    [<AbstractClass>] type Person(name) = memberx.Name = name abstract Greet : unit -> unit type Student(name, studentID : int) = inherit Person(name) member x.StudentID = studentID override x.Greet() = printfn "Student %s" x.Name type Teacher(name, expertise : string) = inherit Person(name) member x.Expertise = expertise override x.Greet() = printfn "Teacher %s." x.Name let st = new Student("Amit", 1234) let tr = new Teacher("Apurva", "Java") //Overriden Greet st.Greet() tr.Greet()
  • 8.
    [<AbstractClass>] type AbstractClass1() = class abstractmember ShowClassName : unit -> unit end type DerivedClass1() = class inherit AbstractClass1() override this.ShowClassName() = printf "This is derived class." end let a = new DerivedClass1() a.ShowClassName()
  • 9.
    Interface • It providespure abstraction. • Abstraction is about hiding unwanted details while showing most essential information. • It is a collection of abstract methods. • A Class which implements interface must provide definition for its all methods.
  • 10.
  • 11.
    Interface Class In aninterface declaration the members are not implemented. The members are abstract, declared by the abstract keyword. We may provide a default implementation using the default keyword. We can implement interfaces either by using object expressions or by using class types. In class or object implementation, We need to provide method bodies for abstract methods of the interface..
  • 12.
    Interface :Syntax [ attributes] type interface-name = [ interface ] [ inherit base-interface-name ...] abstract member1 : [ argument-types1 -> ] return-type1 abstract member2 : [ argument-types2 -> ] return-type2 ... [ end ] The keywords interface and end, which mark the start and end of the definition, are optional
  • 13.
    Interface :Example type Person= abstract Name : string abstract Enter : unit -> unit abstract Leave : unit -> unit
  • 14.
    Calling Interface Methods •Interface methods are called through the interface, • not through the instance of the class or type implementing interface. • To call an interface method, you up cast to the interface type by using the :> operator (upcast operator). Example : (s :> Person).Enter() (s :> Person).Leave()
  • 15.
    Example:1 Person abstract Name abstract Enter() abstract Leave() Student member this.ID member this.Name member this.Enter() member this.Leave() Teacher member this.ID member this.Name member this.Enter() member this.Leave() Implements
  • 16.
    type Person = abstractName : string abstract Enter : unit -> unit abstract Leave : unit -> unit type Student(name : string, id : int) = member this.ID = id interface Person with member this.Name = name member this.Enter() = printfn "Student entering premises!" member this.Leave() = printfn "Student leaving premises!"
  • 17.
    type Teacher(name :string, id : int) = member this.ID = id interface Person with member this.Name = name member this.Enter() = printfn "Teacher entering premises!“ member this.Leave() = printfn "Teacher leaving premises!“ let s = new Student("Amit", 1234) let t = new Teacher("Rohit", 34) (s :> Person).Enter() (s :> Person).Leave() (t :> Person).Enter() (t :> Person).Leave()
  • 18.
    Interface Inheritance Interfaces caninherit from one or more base interfaces. Interface1 abstract member doubleIt Interface2 abstract member tripleIt Interface3 inherit Interface1 inherit Interface2 abstract member printIt() multiplierClass member this.doubleIt(a) member this.tripleIt(a) member this.printIt(a) Implements
  • 19.
    type Interface2 = abstractmember tripleIt: int -> int type Interface3 = inherit Interface1 inherit Interface2 abstract member printIt: int -> string type multiplierClass() = interface Interface3 with member this.doubleIt(a) = 2 * a member this.tripleIt(a) = 3 * a member this.printIt(a) = a.ToString() let ml = multiplierClass() printfn "%d" ((ml:>Interface3).doubleIt(5)) printfn "%d" ((ml:>Interface3).tripleIt(5)) printfn "%s" ((ml:>Interface3).printIt(5))