- F# - Namespaces
- F# - Modules
- F# - Events
- F# - Interfaces
- F# - Inheritance
- F# - Operator Overloading
- F# - Structures
- F# - Classes
- F# - Exception Handling
- F# - Pattern Matching
- F# - Enumerations
- F# - Delegates
- F# - Generics
- F# - Basic I/O
- F# - Mutable Dictionary
- F# - Mutable Lists
- F# - Arrays
- F# - Mutable Data
- F# - Discriminated Unions
- F# - Maps
- F# - Sets
- F# - Sequences
- F# - Lists
- F# - Records
- F# - Tuples
- F# - Options
- F# - Strings
- F# - Functions
- F# - Loops
- F# - Decision Making
- F# - Operators
- F# - Variables
- F# - Data Types
- F# - Basic Syntax
- F# - Program Structure
- F# - Environment Setup
- F# - Overview
- F# - Home
F# Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
F# - Inheritance
One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an apppcation. This also provides an opportunity to reuse the code functionapty and fast implementation time.
When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.
The idea of inheritance implements the IS-A relationship. For example, mammal IS A animal, dog IS-A mammal hence dog IS-A animal as well and so on.
Base Class and Sub Class
A subclass is derived from a base class, which is already defined. A subclass inherits the members of the base class, as well as has its own members.
A subclass is defined using the inherit keyword as shown below −
type MyDerived(...) = inherit MyBase(...)
In F#, a class can have at most one direct base class. If you do not specify a base class by using the inherit keyword, the class imppcitly inherits from Object.
Please note −
The methods and members of the base class are available to users of the derived class pke the direct members of the derived class.
Let bindings and constructor parameters are private to a class and, therefore, cannot be accessed from derived classes.
The keyword base refers to the base class instance. It is used pke the self-identifier.
Example
type Person(name) = member x.Name = name member x.Greet() = printfn "Hi, I m %s" x.Name type Student(name, studentID : int) = inherit Person(name) let mutable _GPA = 0.0 member x.StudentID = studentID member x.GPA with get() = _GPA and set value = _GPA <- value type Teacher(name, expertise : string) = inherit Person(name) let mutable _salary = 0.0 member x.Salary with get() = _salary and set value = _salary <- value member x.Expertise = expertise //using the subclasses let p = new Person("Mohan") let st = new Student("Zara", 1234) let tr = new Teacher("Mariam", "Java") p.Greet() st.Greet() tr.Greet()
When you compile and execute the program, it yields the following output −
Hi, I m Mohan Hi, I m Zara Hi, I m Mariam
Overriding Methods
You can override a default behavior of a base class method and implement it differently in the subclass or the derived class.
Methods in F# are not overridable by default.
To override methods in a derived class, you have to declare your method as overridable using the abstract and default keywords as follows −
type Person(name) = member x.Name = name abstract Greet : unit -> unit default x.Greet() = printfn "Hi, I m %s" x.Name
Now, the Greet method of the Person class can be overridden in derived classes. The following example demonstrates this −
Example
type Person(name) = member x.Name = name abstract Greet : unit -> unit default x.Greet() = printfn "Hi, I m %s" x.Name type Student(name, studentID : int) = inherit Person(name) let mutable _GPA = 0.0 member x.StudentID = studentID member x.GPA with get() = _GPA and set value = _GPA <- value override x.Greet() = printfn "Student %s" x.Name type Teacher(name, expertise : string) = inherit Person(name) let mutable _salary = 0.0 member x.Salary with get() = _salary and set value = _salary <- value member x.Expertise = expertise override x.Greet() = printfn "Teacher %s." x.Name //using the subclasses let p = new Person("Mohan") let st = new Student("Zara", 1234) let tr = new Teacher("Mariam", "Java") //default Greet p.Greet() //Overriden Greet st.Greet() tr.Greet()
When you compile and execute the program, it yields the following output −
Hi, I m Mohan Student Zara Teacher Mariam.
Abstract Class
At times you need to provide an incomplete implementation of an object, which should not be implemented in reapty. Later, some other programmer should create subclasses of the abstract class to a complete implementation.
For example, the Person class will not be needed in a School Management System. However, the Student or the Teacher class will be needed. In such cases, you can declare the Person class as an abstract class.
The AbstractClass attribute tells the compiler that the class has some abstract members.
You cannot create an instance of an abstract class because the class is not fully implemented.
The following example demonstrates this −
Example
[<AbstractClass>] type Person(name) = member x.Name = name abstract Greet : unit -> unit type Student(name, studentID : int) = inherit Person(name) let mutable _GPA = 0.0 member x.StudentID = studentID member x.GPA with get() = _GPA and set value = _GPA <- value override x.Greet() = printfn "Student %s" x.Name type Teacher(name, expertise : string) = inherit Person(name) let mutable _salary = 0.0 member x.Salary with get() = _salary and set value = _salary <- value member x.Expertise = expertise override x.Greet() = printfn "Teacher %s." x.Name let st = new Student("Zara", 1234) let tr = new Teacher("Mariam", "Java") //Overriden Greet st.Greet() tr.Greet()
When you compile and execute the program, it yields the following output −
Student Zara Teacher Mariam.Advertisements