Advanced Object-Oriented Programming Features

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Inheritance Inheritance Reserved word protected Reserved word super
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Programming Based on Events
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Chapter 10 Inheritance, Polymorphism, and Scope. 2 Knowledge Goals Understand the hierarchical nature of classes in object-oriented programming Understand.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
C# Programming: From Problem Analysis to Program Design1 2 Your First C# Program C# Programming: From Problem Analysis to Program Design 2 nd Edition.
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Introduction to Computing and Programming
C# Programming: From Problem Analysis to Program Design1 2 Your First C# Program.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
C# Programming: From Problem Analysis to Program Design1 10 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Chapter 13: Object-Oriented Programming
Chapter 10 Classes Continued
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
A First Program Using C#
Chapter 12: Adding Functionality to Your Classes.
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.
Microsoft Visual Basic 2005: Reloaded Second Edition
Module 7: Object-Oriented Programming in Visual Basic .NET
An Object-Oriented Approach to Programming Logic and Design
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Inheritance in the Java programming language J. W. Rider.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Inheritance Building one object from another. Background Object-oriented programming is normally described has offering three capabilities Encapsulation:
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
1 Chapter Nine Using GUI Objects and the Visual Studio IDE.
11/23/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 35 Title: C# vs. Java Inheritance & Polymorphism Reference: COS240 Syllabus.
C# Programming: From Problem Analysis to Program Design1 10 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Creating Your Own Classes
INF230 Basics in C# Programming
Advanced Object-Oriented Programming Features
Inheritance and Polymorphism
Inheritance & Polymorphism Reference: COS240 Syllabus
Advanced Object-Oriented Programming Features
Using GUI Objects and the Visual Studio IDE
Advanced Object-Oriented Programming Features
C# Programming: From Problem Analysis to Program Design
OOP’S Concepts in C#.Net
One class is an extension of another.
Social Media And Global Computing Introduction to Visual Studio
Advanced Java Topics Chapter 9
Social Media And Global Computing Using DLLs with MVC
Java Programming, Second Edition
Presentation transcript:

Advanced Object-Oriented Programming Features 11 C# Programming: From Problem Analysis to Program Design 3rd Edition C# Programming: From Problem Analysis to Program Design

Chapter Objectives Learn the major features of object-oriented languages Design and develop multitier applications using component-based development methods Use inheritance to extend the functionality of user- defined classes Create abstract classes that include abstract methods C# Programming: From Problem Analysis to Program Design

Chapter Objectives (continued) Distinguish the differences between sealed and abstract classes Become aware of partial classes Design and implement interfaces Understand why polymorphic programming is a common goal in .NET Explore generics and learn how to create generic classes and generic methods C# Programming: From Problem Analysis to Program Design

Chapter Objectives (continued) Investigate static versus dynamic typing and become aware of when dynamic and var types are used Work through a programming example that illustrates the chapter’s concepts C# Programming: From Problem Analysis to Program Design

Object-Oriented Language Features Abstraction Abstract or identify the objects involved in the problem Encapsulation Packaging data and behaviors into a single unit Inheritance Reuse of code through extending program units Polymorphism Multiple implementations of the same behaviors C# Programming: From Problem Analysis to Program Design

Component-Based Development Figure 11-1 Component-based development C# Programming: From Problem Analysis to Program Design

Component-Based Development (continued) Multitier applications Data access tier for accessing data from text files and databases Graphical user interface tier for user interaction Windows Web Components implemented through classes in C# Class library files with a dynamic link library (DLL) extension C# Programming: From Problem Analysis to Program Design

Inheritance Enables you to: Associated with an "is a" relationship Create a general class and then define specialized classes that have access to the members of the general class Associated with an "is a" relationship Specialized class “is a” form of the general class Classes can also have a "has a" relationship, not associated with inheritance "has a" relationship is associated with containment or aggregation C# Programming: From Problem Analysis to Program Design

Inheriting from the Object Class Every object inherits four methods as long as reference to the System namespace included Figure 11-2 Methods inherited from an object C# Programming: From Problem Analysis to Program Design

Inheriting from Other .NET FCL Classes Add functionality to programs with minimal programming Extend System.Windows.Forms.Form class to build GUIs (Button, Label, TextBox, ListBox) Base class Derived class Figure 11-3 Derived class C# Programming: From Problem Analysis to Program Design

Creating Base Classes for Inheritance Can define your own classes from which other classes can inherit Base class is called the super or parent class Data members are defined with a private access modifier Constructors are defined with public access modifiers Properties offer public access to data fields C# Programming: From Problem Analysis to Program Design

Overriding Methods Replace the method defined at a higher level Keyword override included in derived class Base method includes virtual, abstract, or override keyword Overriding differs from overloading a method Overridden methods have exactly the same signature Overloaded methods each have a different signature Figure 11-4 ToString( ) signature C# Programming: From Problem Analysis to Program Design

Overriding Methods (continued) Example of polymorphism ToString( ) method can have many different definitions ToString( ) uses the virtual modifier, implying that any class can override it Derived classes inherit from a base class Also called subclasses or child classes Protected access modifiers Access only to classes that derived from them Access to change data in the base class C# Programming: From Problem Analysis to Program Design

Calling the Base Constructor To call the constructor for the base class, add keyword :base between the constructor heading for the subclass and the opening curly brace public Student(string id, string fname, string lname, string maj, int sId) :base (id, lname, fname) // base constructor arguments { . . . Base constructor must have a constructor with matching signature C# Programming: From Problem Analysis to Program Design

Using Members of the Base Class Scope Methods defined in subclass take precedence when named the same name as member of a parent class Can call an overridden method of the base class Use keyword base before the method name return base.GetSleepAmt( ) // Calls GetSleepAmt( ) in // parent class C# Programming: From Problem Analysis to Program Design

Relationship between the Person and Student Classes Figure 11-5 Inheritance class diagram C# Programming: From Problem Analysis to Program Design

Making Stand-Alone Components Compile class and create an assembly Assemblies are units configured and deployed in .NET Classes can be compiled and stored as a dynamic link library (DLL) instead of into the EXE file type Adds a reference to the DLL That referenced file with the .dll extension becomes part of the application’s private assembly C# Programming: From Problem Analysis to Program Design

Using Visual Studio to Create DLL Files Figure 11-6 Creating a DLL component C# Programming: From Problem Analysis to Program Design

Build Instead of Run to Create DLL Create the parent class first in order to use IntelliSense Create the subclass class in the same way as usual, except Build instead of Run the project to create the DLL Figure 11-7 Attempting to run a class library file C# Programming: From Problem Analysis to Program Design

Add Reference to Base Class One of the first things to do is Add a Reference to the Parent DLL Figure 11-8 Adding a reference to a DLL C# Programming: From Problem Analysis to Program Design

Add Reference to Base Class (continued) Use Browse button to locate DLL Figure 11-9 Add Reference dialog box C# Programming: From Problem Analysis to Program Design

Add Reference to Base Class (continued) Figure 11-10 Locating the Person.dll component C# Programming: From Problem Analysis to Program Design

Adding a New Using Statement In the subclass class, if you simply type the following, you receive an error message public class Student : Person Figure 11-11 Namespace reference error C# Programming: From Problem Analysis to Program Design

Adding a New Using Statement (continued) Notice fully qualified name To avoid error, could type: public class Student : PersonNamespace.Person Better option is to add a using directive using PersonNamespace; // Use whatever name you // typed for the namespace for Person After typing program statements, build the DLL from the Build option under the Build menu bar C# Programming: From Problem Analysis to Program Design

Creating a Client Application to Use the DLL DLL components can be reused with many different applications Two steps Add a reference to the DLL components Include a using statement with the namespace Then declare an object of the component type(s) Use members of the derived, base, or referenced classes C# Programming: From Problem Analysis to Program Design

Creating a Client Application to Use the DLL (continued) Figure 11-12 DLLs referenced in the PresentationGUI class C# Programming: From Problem Analysis to Program Design

Creating a Client Application to Use the DLL (continued) Figure 11-13 PresentationGUI output referencing two DLLs C# Programming: From Problem Analysis to Program Design

Using ILDASM to View the Assembly (ILDASM): Intermediate Language Disassembler tool Assembly shows the signatures of all methods, data fields, and properties One option – display the source code as a comment in the assembly Can be run from the command line or from within the Visual Studio IDE Must be added as an external tool in Visual Studio C# Programming: From Problem Analysis to Program Design

ILDASM to View the Assembly .ctors are constructors IL code for the method Data fields Properties converted to methods Figure 11-14 Student.dll assembly from ILDASM C# Programming: From Problem Analysis to Program Design