Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Object Oriented Concepts - Programming Using C Sharp - Lecture Slides, Slides of Information Security and Markup Languages

The main points in the computer security, which are very important in the context of security are:Virtual Lans, Territory, Broadcast Domain, Segments, Physical Location, Common, Communicate, Related Standards, Same Switch, Forwarding Packets

Typology: Slides

2012/2013

Uploaded on 04/22/2013

sathaye
sathaye 🇮🇳

4.8

(8)

106 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Classes II
Object Oriented Concepts
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Object Oriented Concepts - Programming Using C Sharp - Lecture Slides and more Slides Information Security and Markup Languages in PDF only on Docsity!

Classes II

Object Oriented Concepts

Object Oriented concepts

• Encapsulation

• Composition

• Inheritance

• Polymorphism

Composition

  • A class can have references to objects of other classes as members. This is called composition and is sometimes referred to as a has-a relationship
  • One form of software reuse is composition
  • Example: Employee object using Date object for hire date
    • See composition.cs

Inheritance

  • Another form of software reuse is inheritance
  • Inheritance allows a new class to absorb an existing class’s members.
  • A derived class normally adds its own fields and methods to represent a more specialized group of objects.
  • The is-a relationship represents inheritance.
  • Examples:

Base class Derived classes Student GraduateStudent, UndergraduateStudent Shape Circle, Triangle, Rectangle Loan CarLoan, HomeImprovementLoan, MortgageLoan Employee Faculty, Staff, HourlyWorker, CommissionWorker BankAccount CheckingAccount, SavingsAccount

Base and Derived Classes in C#

  • Objects of all classes that extend a common base class can be treated as objects of that base class.
  • A base class’s private members are not directly accessible by derived-class methods and properties.
  • A base class’s protected members can be accessed by members of both that base class and of its derived classes.
  • A base class’s protected internal members can be accessed by members of a base class, the derived classes and by any class in the same assembly.
  • If a class does not specify that it inherits from another class, it implicitly inherits from object. public class CommissionEmployee : object

Base and Derived Classes in C#

  • The derived class can override the base-class method. To override a base-class method, a derived class must declare a method with keyword override. public override string ToString()
  • A constructor initializer with keyword base invokes the base-class constructor. - See example BasePlusCommissionEmployee
  • The virtual and abstract keywords are required in a base-class method so that the derived classes can override it.
  • See example BasePlusCommissionEmployee
  • Using protected instance variables creates several potential problems.
    • The derived-class object can set an inherited variable’s value directly without validity checking.
    • Derived-class methods would need to be written to depend on the base class’s data implementation.
  • You should be able to change the base-class implementation while still providing the same services to the derived classes.

System.Object

Method Description Equals (^) This method compares two objects for equality and returns true if they are equal and false otherwise. Finalize (^) Finalize is called by the garbage collector before it reclaims an object’s memory. GetHashCode (^) The hashcode value returned can be used by a hashtable to determine the location at which to insert the corresponding value. GetType (^) Returns an object of class Type that contains information about the object’s type. MemberwiseClone (^) This protected method makes a copy of the object on which it is called. Instance-variable values in one object are copied into another object of the same type. For reference types, only the references are copied. ReferenceEquals (^) This static method returns true if two objects are the same instance or if they are null references. ToString (^) Returns a string representation of an object. The default implementation returns the namespace and class name.

  • All classes inherit directly or indirectly from the object class.
  • Figure below summarizes object’s methods:

Polymorphism

  • Polymorphism enables you to write applications that process objects that share the same base class in a class hierarchy as if they were all objects of the base class.
  • If class Rectangle is derived from class Quadrilateral, then a Rectangle is a more specific version of a Quadrilateral.
  • Any operation that can be performed on a Quadrilateral object can also be performed on a Rectangle object.
  • These operations also can be performed on other Quadrilaterals, such as Squares, Parallelograms and Trapezoids.
  • The polymorphism occurs when an application invokes a method through a base-class variable.

Case Study: Payroll System Using

Polymorphism

  • In this example, we create an enhanced employee hierarchy to solve the following problem: A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid a fixed weekly salary regardless of the number of hours worked, hourly employees are paid by the hour and receive overtime pay for all hours worked in excess of 40 hours, commission employees are paid a percentage of their sales, and salaried-commission employees receive a base salary plus a percentage of their sales. For the current pay period, the company has decided to reward salaried- commission employees by adding 10% to their base salaries.

Payroll System Using Polymorphism

  • We use abstract class Employee to represent the general concept of an employee.
  • SalariedEmployee , CommissionEmployee and HourlyEmployee extend Employee.
  • Class BasePlusCommissionEmployee —which extends CommissionEmployee —represents the last employee type.
  • Let’s look at the example code: PayrollSystem solution

Figure | Employee hierarchy UML class diagram

Abstract Classes

  • abstract property declarations have the form: public abstract PropertyType MyProperty { get; set; } // end abstract property
  • An abstract property may omit implementations for the get accessor, the set accessor or both.
  • Concrete derived classes must provide implementations for every accessor declared in the abstract property.

as / is operators

  • When downcasting an object, a System.InvalidCastException occurs if at execution time the object does not have an is-a relationship with the type specified in the cast operator. An object can be cast only to its own type or to the type of one of its base classes.
  • You can avoid a potential InvalidCastException by using the as operator to do a downcast rather than a cast operator. - If the downcast is invalid , the expression will be null instead of throwing an exception.
  • Before performing a cast from a base-class object to a derived- class object, use the is operator to ensure that the object is indeed an object of an appropriate derived-class type.

Interfaces

  • An interface is typically used when disparate (i.e., unrelated) classes need to share common methods so that they can be processed polymorphically
  • A programmer can create an interface that describes the desired functionality, then implement this interface in any classes requiring that functionality.
  • An interface often is used in place of an abstract class when there is no default implementation to inherit—that is, no fields and no default method implementations.
  • Like abstract classes, interfaces are typically public types, so they are normally declared in files by themselves with the same name as the interface and the .cs file-name extension.

Interface Example: IPayable

  • To build an application that can determine payments for employees and invoices alike, we first create an interface named IPayable.
  • Interface IPayable contains method GetPaymentAmount that returns a decimal amount to be paid for an object of any class that implements the interface.
  • By convention, the name of an interface begins with "I". This helps distinguish interfaces from classes, improving code readability.
  • When a class implements an interface, the same is-a relationship provided by inheritance applies.
  • Example: IPayable solution