08 iec t1_s1_oo_ps_session_11

16
Slide 1 of 16 Session 11 Ver. 1.0 Object-Oriented Programming Using C# In this session, you will learn to: Use abstract classes Use sealed classes Use interfaces Implement the file input and output operations Objectives

Transcript of 08 iec t1_s1_oo_ps_session_11

Page 1: 08 iec t1_s1_oo_ps_session_11

Slide 1 of 16Session 11Ver. 1.0

Object-Oriented Programming Using C#

In this session, you will learn to:Use abstract classes

Use sealed classes

Use interfaces

Implement the file input and output operations

Objectives

Page 2: 08 iec t1_s1_oo_ps_session_11

Slide 2 of 16Session 11Ver. 1.0

Object-Oriented Programming Using C#

C# enables you to create abstract classes that are used to provide partial class implementation of an interface.

Abstract classes contain abstract methods, which can be implemented by the derived class.

Polymorphism can be implemented by using abstract classes and virtual functions.

There are certain rules governing the use of an abstraction class:

Cannot create an instance of an abstract class.

Cannot declare an abstract method outside an abstract class.

Cannot be declared sealed.

Using Abstract Classes

Page 3: 08 iec t1_s1_oo_ps_session_11

Slide 3 of 16Session 11Ver. 1.0

Object-Oriented Programming Using C#

Using Abstract Methods

Abstract methods are methods without any body.

The implementation of an abstract method is done by the derived class.

When a derived class inherits the abstract method form the abstract class, it must override the abstract methods. This requirement is enforced at compile time, and is also called dynamic polymorphism.

The syntax for using the abstract method is as follows:[access-modifiers] abstract return-type method name (parameters]);

The abstract method is declared by adding the abstract modifier to the method.

Page 4: 08 iec t1_s1_oo_ps_session_11

Slide 4 of 16Session 11Ver. 1.0

Object-Oriented Programming Using C#

Using Virtual Functions

When you have a function defined in a class which you want to allow to be implemented by the inherited classes, you can use virtual function.

The virtual function could be implemented by the inherited classes in their own way and the call to the method is decided at the run time.

Page 5: 08 iec t1_s1_oo_ps_session_11

Slide 5 of 16Session 11Ver. 1.0

Object-Oriented Programming Using C#

Using Sealed Classes

You could restrict users form inheriting the class by sealing the class using the sealed keyword.

The sealed keyword tells the compiler that the class is sealed, and therefore, cannot be extended.

The following is an example of a sealed class:sealed class FinalClass

{

private int x;

public void Method1()

{

}

}

A method can also be sealed and in that case the method cannot be overridden.

Page 6: 08 iec t1_s1_oo_ps_session_11

Slide 6 of 16Session 11Ver. 1.0

Object-Oriented Programming Using C#

Using Interfaces

Interfaces define properties, methods, and events, which are known as the members of the interface.

Interfaces are fully supported by C#.

Interfaces are used when a standard structure of methods is to be followed by the classes, and where classes will implement the functionality.

Interfaces separate the definition of objects from their implementation so that the objects can evolve without the risk of introducing incompatibility in existing applications.

Page 7: 08 iec t1_s1_oo_ps_session_11

Slide 7 of 16Session 11Ver. 1.0

Object-Oriented Programming Using C#

Working with Interfaces

Working with interfaces includes interface declaration and implementation of interface by the classes.

You can declare interfaces using the interface keyword.

Interface statements are public, by default.

You can declare only methods, functions, and properties in interfaces. You cannot declare a variable in interfaces.

Interfaces declare methods, which are implemented by classes. A class can inherit from single class but can implement form multiple interfaces.

Page 8: 08 iec t1_s1_oo_ps_session_11

Slide 8 of 16Session 11Ver. 1.0

Object-Oriented Programming Using C#

Inheriting Interfaces

A class or a structure that implements interfaces also implements the base interfaces of the inherited interface.

Page 9: 08 iec t1_s1_oo_ps_session_11

Slide 9 of 16Session 11Ver. 1.0

Object-Oriented Programming Using C#

Problem Statement:Furniture and Fittings Company (FFC) manufactures domestic furniture. Customers provide their specifications to the company for the furniture they want. To cope with the received customer’s orders, FFC decides to computerize the order-processing system. The system should accept the values of furniture items, such as a bookshelf and a chair. You need to develop the hierarchy of these items.

Demo: Order-Processing System Using Abstract Classes

Page 10: 08 iec t1_s1_oo_ps_session_11

Slide 10 of 16Session 11Ver. 1.0

Object-Oriented Programming Using C#

Solution:To create a console based application for FFC, you need to perform the following tasks:

1. Create a console-based application.

2. Build and execute an application.

Demo: Order-Processing System Using Abstract Classes (Contd.)

Page 11: 08 iec t1_s1_oo_ps_session_11

Slide 11 of 16Session 11Ver. 1.0

Object-Oriented Programming Using C#

The stream is a sequence of bytes traveling from a source to a destination over a communication path.

The two basic streams used are the input and output streams.

An input stream is used for a read operation and an output stream is used for performing a write operation.

The System.IO namespace includes various classes, which are used to perform operations, such as file creation, file deletion, and the read-write operations to files.

Implementing the File Input and Output Operations

Page 12: 08 iec t1_s1_oo_ps_session_11

Slide 12 of 16Session 11Ver. 1.0

Object-Oriented Programming Using C#

To open an existing file or to create a new file, you need to create an object of type FileStream.

Consider the following syntax for creating the object of type FileStream:

FileStream <object name>=new FileStream(<file Name>,<FileMode Enumerator>,<File Access Enumerator>,<FileShare Enumerator>);

FileStream Class

Page 13: 08 iec t1_s1_oo_ps_session_11

Slide 13 of 16Session 11Ver. 1.0

Object-Oriented Programming Using C#

FileMode enumerator defines various methods for opening files.

The FileMode enumerator parameter is specified in many constructors for the FileStream.

The parameters to FileMode checks whether a file is overwritten, created, or opened.

FileAccess enumerator indicates whether you want to read data from the file, write to the file, or perform both the operations.

The members of the FileAccess enumerator are Read, ReadWrite, and Write.

FileStream Class (Contd.)

Page 14: 08 iec t1_s1_oo_ps_session_11

Slide 14 of 16Session 11Ver. 1.0

Object-Oriented Programming Using C#

The FileShare enumerator contains constants for controlling the kind of access that the other FileStream constructors can have to the same file.

A typical use of this enumeration is to define whether two different applications can simultaneously read from the same file.

FileStream Class (Contd.)

Page 15: 08 iec t1_s1_oo_ps_session_11

Slide 15 of 16Session 11Ver. 1.0

Object-Oriented Programming Using C#

In this session, you learned that:An abstract superclass is a conceptual class that does not exist in real world but acts as a base from which other classes inherit properties behavior.

A sealed class tells the compiler that the class cannot be extended further. You cannot derive a class from a sealed class.

An interface defines properties, methods, and events. The properties, methods, and events that are defined in the interface are known as the members of the interface.

A stream is an abstraction of a sequence of bytes traveling from a source to a destination over a communication path.

Summary

Page 16: 08 iec t1_s1_oo_ps_session_11

Slide 16 of 16Session 11Ver. 1.0

Object-Oriented Programming Using C#

The two basic streams used are the input and output streams. An input stream is used for a read operation and an output stream is used for performing a write operation.

Most file input/output (I/O) support in the .NET Framework is implemented in the System.IO namespace. You can use the FileStream class in the System.IO namespace to read from, to write, and to close files.

The FileStream class inherits from the abstract class Stream.

Summary (Contd.)