Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Final and Abstract Classes
OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Road Map Introduction to object oriented programming. Classes
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Inheritance using Java
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Lecture 9 Polymorphism Richard Gesick.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14.
Neal Stublen What’s an indexer?  An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Module 10: Inheritance in C#. Overview Deriving Classes Implementing Methods Using Sealed Classes Using Interfaces Using Abstract Classes.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Neal Stublen Tonight’s Agenda  Indexers  Delegates and events  Operator overloading  Class inheritance  Q&A.
COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.
Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke.
Introduction to Object-Oriented Programming Lesson 2.
Interfaces An interface is like an extreme case of an abstract class – However, an interface is not a class – It is a type that can be satisfied by any.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Properties and Indexers 9. © Aptech Ltd.Building Applications Using C# / Session 92 Objectives  Define properties in C#  Explain properties, fields,
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Object-Oriented Programming: Inheritance and Polymorphism.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Module 13: Properties and Indexers. Overview Using Properties Using Indexers.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter - 4 OOP with C# S. Nandagopalan.
2.7 Inheritance Types of inheritance
Table of Contents Class Objects.
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
CS360 Windows Programming
Object Oriented Analysis and Design
Interfaces.
OOP’S Concepts in C#.Net
Interface.
Abstract Classes AKEEL AHMED.
Interfaces.
Fundaments of Game Design
Understanding Polymorphism
CIS 199 Final Review.
Class.
5. 3 Coding with Denotations
Final and Abstract Classes
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 2 of 34 Module 6 - Review

Abstract classes & Interface & Properties & Indexers / Session 4 / 3 of 34 Module 7 - Review

Abstract classes & Interface & Properties & Indexers / Session 4 / 4 of 34 Module 8 - Objectives  Describe how implement abstract classes  Define abstracts methods  Describe how implement interface  List the differences between abstract class and Interface

Abstract classes & Interface & Properties & Indexers / Session 4 / 5 of 34 Abstract Base Classes  Abstract classes are classes that can be inherited from  Abstract base classes cannot be instantiated nor can they be sealed  C# allows creation of Abstract Base classes by an addition of the abstract modifier to the class definition  An abstract method is defined in the abstract base class and its actual implementation is written in the derived class

Abstract classes & Interface & Properties & Indexers / Session 4 / 6 of 34 Abstract Base Classes - Example using System; abstract class BaseClass { public abstract void MethodA(); public void MethodB() { Console.WriteLine ("This is the non abstract method”); } } class DerivedClass : BaseClass { public override void MethodA() { Console.WriteLine ("This is the abstract method overriden in derived class"); }

Abstract classes & Interface & Properties & Indexers / Session 4 / 7 of 34 Abstract Base Classes - Example class AbstractDemo { public static void Main() { DerivedClass objDerived = new DerivedClass(); BaseClass objBase = objDerived; objBase.MethodA(); objDerived.MethodB(); }

Abstract classes & Interface & Properties & Indexers / Session 4 / 8 of 34 Interfaces  An interface is a pure abstract base class  It can contain only abstract methods and no method implementation  A class that implements a particular interface must implement the members listed by that interface public interface IFile { int delFile(); void disFile(); }

Abstract classes & Interface & Properties & Indexers / Session 4 / 9 of 34 Interfaces - Example public interface IFile { int delFile(); void disFile(); } public class MyFile : IFile { public int delFile() { System.Console.WriteLine ("DelFile Implementation!"); return(0); } public void disFile() { System.Console.WriteLine ("DisFile Implementation!"); }

Abstract classes & Interface & Properties & Indexers / Session 4 / 10 of 34 Interfaces - Output class InterfaceDemo { public static void Main() { MyFile objMyFile = new MyFile(); objMyFile.disFile(); int retValue = objMyFile.delFile(); }

Abstract classes & Interface & Properties & Indexers / Session 4 / 11 of 34 Multiple Interfaces  C# allows multiple interface implementations public interface IFileTwo { void applySecondInterface(); }

Abstract classes & Interface & Properties & Indexers / Session 4 / 12 of 34 Multiple Interfaces - Example public class MyFile : BaseforInterface, IFile, IFileTwo { public int delFile() { System.Console.WriteLine ("DelFile Implementation!"); return(0); } public void disFile() { System.Console.WriteLine ("DisFile Implementation!"); } public void applySecondInterface() { System.Console.WriteLine ("ApplySecondInterface Implementation!"); }

Abstract classes & Interface & Properties & Indexers / Session 4 / 13 of 34 Multiple Interfaces - Output class MultipleInterfaces { public static void Main() { MyFile objMyFile = new MyFile(); objMyFile.disFile(); int retValue = objMyFile.delFile(); objMyFile.open(); objMyFile.applySecondInterface(); }

Abstract classes & Interface & Properties & Indexers / Session 4 / 14 of 34 Explicit Interface (1)  Explicit interface implementation can be used when a method with the same name is available in 2 interfaces public interface IFile { int delFile(); void disFile(); } public interface IFileTwo { void applySecondInterface(); void disFile(); }

Abstract classes & Interface & Properties & Indexers / Session 4 / 15 of 34 Explicit Interface (2) public class MyFile : BaseforInterface, IFile, IFileTwo {... void IFile.disFile() { System.Console.WriteLine ("IFile Implementation of DisFile"); } void IFileTwo.disFile() { System.Console.WriteLine ("IFileTwo Implementation of DisFile"); }.. }

Abstract classes & Interface & Properties & Indexers / Session 4 / 16 of 34 Interface Inheritance  New Interfaces can be created by combining together other interfaces  The syntax for this is similar to that used for inheritance, except that more than one interface can be merged to form a single interface. interface IAllFile : IFile, IFileTwo { //More operations can be added if necessary //(apart from that of IFile & IFileTwo) }

Abstract classes & Interface & Properties & Indexers / Session 4 / 17 of 34 The differences between abstract classes and interface

Abstract classes & Interface & Properties & Indexers / Session 4 / 18 of 34 Module 8 - Summary

Abstract classes & Interface & Properties & Indexers / Session 4 / 19 of 34 Module 9 - Objectives  Define properties in C#  List and explain the types of properties  State and explain indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 20 of 34 Properties  Access modifier like public, private, protected, internal are used to control the accessibility of fields and methods in c#  The public field are accessible by other class but private fields are accessible only by the class in which they are declared  C# use feature called properties that allows you to set and retrieve value of fields declared with any access modifier in secured manner.

Abstract classes & Interface & Properties & Indexers / Session 4 / 21 of 34 Syntax

Abstract classes & Interface & Properties & Indexers / Session 4 / 22 of 34 Properties  The get accessor is used to read a value  The set accessor is use to assign a value

Abstract classes & Interface & Properties & Indexers / Session 4 / 23 of 34 Read only property To retrieve the value of a private field Need define the get accessor

Abstract classes & Interface & Properties & Indexers / Session 4 / 24 of 34 Write only property To change the value of a private field Need define the set accessor

Abstract classes & Interface & Properties & Indexers / Session 4 / 25 of 34 Read and Write property To allow set and retrieve the value of private field Need define set and get accessor

Abstract classes & Interface & Properties & Indexers / Session 4 / 26 of 34 Use of properties  Modifies private field  Validates private field  Perform required actions  Implements abstraction  Implements encapsulation

Abstract classes & Interface & Properties & Indexers / Session 4 / 27 of 34 Properties vs. Fields

Abstract classes & Interface & Properties & Indexers / Session 4 / 28 of 34 Property vs. Method

Abstract classes & Interface & Properties & Indexers / Session 4 / 29 of 34 Indexers  Indexers are data members that allow you to access data within objects in a way that is similar to accessing arrays

Abstract classes & Interface & Properties & Indexers / Session 4 / 30 of 34 Syntax of index declaration

Abstract classes & Interface & Properties & Indexers / Session 4 / 31 of 34 Parameters  Indexers must have at least one parameter.  The parameter denotes the index. position, using index position the stored value at that position is set or accessed.  Index can also have multiple parameters. Such indexers can be accessed like a multi dimensional array.

Abstract classes & Interface & Properties & Indexers / Session 4 / 32 of 34 Properties versus Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 33 of 34 Module 9 – Summary (1)  A field is a data member of class that store some information.  Properties enables to you to access the private fields of class.  Methods are data members that define a behavior performed by an object.  Properties protect fields of the class while accessing them.

Abstract classes & Interface & Properties & Indexers / Session 4 / 34 of 34 Module 9 - Summary (2)  Property accessor enable you to read and assign values to fields  Property can be created by using get and set accessors  Indexers treats an object like an array, so providing faster access to data within the object.