SlideShare a Scribd company logo
1 of 19
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))

More Related Content

What's hot

What's hot (20)

06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Java OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceJava OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & Interface
 
Java basics and java variables
Java basics and java variablesJava basics and java variables
Java basics and java variables
 
Abstract classes
Abstract classesAbstract classes
Abstract classes
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younas
 
OOP Concepets and UML Class Diagrams
OOP Concepets and UML Class DiagramsOOP Concepets and UML Class Diagrams
OOP Concepets and UML Class Diagrams
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
CLASS & OBJECT IN JAVA
CLASS & OBJECT  IN JAVACLASS & OBJECT  IN JAVA
CLASS & OBJECT IN JAVA
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Classes&amp;objects
Classes&amp;objectsClasses&amp;objects
Classes&amp;objects
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
 
Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Chapter 9 Abstract Class
Chapter 9 Abstract ClassChapter 9 Abstract Class
Chapter 9 Abstract Class
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 

Similar to .NET F# Abstract class interface

06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programming06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programming
deffa5
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
Getachew Ganfur
 
Inheritance & Polymorphism - 2
Inheritance & Polymorphism - 2Inheritance & Polymorphism - 2
Inheritance & Polymorphism - 2
PRN USM
 

Similar to .NET F# Abstract class interface (20)

.NET F# Events
.NET F# Events.NET F# Events
.NET F# Events
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 
.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading
 
Abstract class and interface
Abstract class and interfaceAbstract class and interface
Abstract class and interface
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and Interfaces
 
06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programming06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programming
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt
 
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdfch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
C# program structure
C# program structureC# program structure
C# program structure
 
Basic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptxBasic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptx
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
Java interface
Java interfaceJava interface
Java interface
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 
Inheritance & Polymorphism - 2
Inheritance & Polymorphism - 2Inheritance & Polymorphism - 2
Inheritance & Polymorphism - 2
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 

More from DrRajeshreeKhande

More from DrRajeshreeKhande (18)

Exception Handling in .NET F#
Exception Handling in .NET F#Exception Handling in .NET F#
Exception Handling in .NET F#
 
.NET F# Class constructor
.NET F# Class constructor.NET F# Class constructor
.NET F# Class constructor
 
.Net F# Generic class
.Net F# Generic class.Net F# Generic class
.Net F# Generic class
 
F# Console class
F# Console classF# Console class
F# Console class
 
.net F# mutable dictionay
.net F# mutable dictionay.net F# mutable dictionay
.net F# mutable dictionay
 
F sharp lists & dictionary
F sharp   lists &  dictionaryF sharp   lists &  dictionary
F sharp lists & dictionary
 
F# array searching
F#  array searchingF#  array searching
F# array searching
 
Net (f#) array
Net (f#)  arrayNet (f#)  array
Net (f#) array
 
.Net (F # ) Records, lists
.Net (F # ) Records, lists.Net (F # ) Records, lists
.Net (F # ) Records, lists
 
MS Office for Beginners
MS Office for BeginnersMS Office for Beginners
MS Office for Beginners
 
Java Multi-threading programming
Java Multi-threading programmingJava Multi-threading programming
Java Multi-threading programming
 
Java String class
Java String classJava String class
Java String class
 
JAVA AWT components
JAVA AWT componentsJAVA AWT components
JAVA AWT components
 
Dr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWTDr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWT
 
Dr. Rajeshree Khande Java Interactive input
Dr. Rajeshree Khande Java Interactive inputDr. Rajeshree Khande Java Interactive input
Dr. Rajeshree Khande Java Interactive input
 
Dr. Rajeshree Khande :Intoduction to java
Dr. Rajeshree Khande :Intoduction to javaDr. Rajeshree Khande :Intoduction to java
Dr. Rajeshree Khande :Intoduction to java
 
Java Exceptions Handling
Java Exceptions Handling Java Exceptions Handling
Java Exceptions Handling
 
Dr. Rajeshree Khande : Java Basics
Dr. Rajeshree Khande  : Java BasicsDr. Rajeshree Khande  : Java Basics
Dr. Rajeshree Khande : Java Basics
 

Recently uploaded

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 

Recently uploaded (20)

INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 

.NET F# Abstract class interface

  • 2. 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
  • 4. 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.
  • 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) = 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()
  • 8. [<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()
  • 9. 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.
  • 11. 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..
  • 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 = 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!"
  • 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 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
  • 19. 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))