Classes (Part 1) Lecture 3

Slides:



Advertisements
Similar presentations
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Advertisements

Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Object-Oriented Programming Chapter Chapter
Chapter 3 Introduction to Classes and Objects Definitions Examples.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Introduction to Object-Oriented Programming Lesson 2.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 5 Classes Essential C# Mark Michaelis Ch. 51 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: ). Copyright 2010 Pearson Education,
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Eine By: Avinash Reddy 09/29/2016.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Classes C++ representation of an object
Abstract Data Types and Encapsulation Concepts
Review What is an object? What is a class?
2.7 Inheritance Types of inheritance
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Creating Your OwnClasses
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Lecture 4-7 Classes and Objects
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Can perform actions and provide communication
Inheritance Basics Programming with Inheritance
Can perform actions and provide communication
Corresponds with Chapter 7
Lecture 22 Inheritance Richard Gesick.
Packages and Interfaces
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises UTPA – Fall 2012 This set of slides is revised from.
Chapter 9 Objects and Classes
Learning Objectives Classes Constructors Principles of OOP
Computer Programming with JAVA
Can perform actions and provide communication
The University of Texas – Pan American
COP 3330 Object-oriented Programming in C++
Fundaments of Game Design
CIS 199 Final Review.
Classes C++ representation of an object
OO Programming Concepts
Chapter 11 Inheritance and Encapsulation and Polymorphism
Classes and Objects Systems Programming.
Creating and Using Classes
CSG2H3 Object Oriented Programming
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Classes (Part 1) Lecture 3 M.T. Ipalakova

Object-Oriented Programming (OOP) Object-oriented programming helps you think about the problem you want to solve and gives you a way to represent, or model, that problem in your code The main principles of OOP are encapsulation, inheritance and polymorphism A class is the fundamental concept of OOP, which defines both representation and behavior in a single unit Classes are the primary mechanism you use to create user-defined types

Classes Classes in C# are reference types that implicitly derive from object To define a class, use the class keyword The body of the class enclosed in the opening and closing braces The body contains the definition of the data and behavior for the class The main class members are fields, properties and methods class Person { }

Accessibility Accessibility enables you to control the visibility, or accessibility, of an entity outside of its containing scope C# provides this through access modifiers Namespaces are not allowed to have any access modifiers and are always public Classes default to internal accessibility but are allowed to have either public or internal declared accessibility Class members default to private accessibility but can have any of the four kinds of declared accessibility

Access Modifiers public – access is not limited protected – access is limited to the containing class or classes derived from the containing class internal – access is limited to the containing assembly private – access is limited to the containing class only

Fields Fields are variables that represent data associated with a class Must be defined in the outermost scope of a class Can be either an instance field or a static field Any access modifier can be specified, but typically, fields are private, which is the default If a field is not given an initial value when it is declared, it is assigned the default value appropriate for its type

Fields class Person { string firstName; string lastName; int yearOfBirth; bool gender; }

Properties A property provides a simple way to access a private field, called the backing field, which can be publicly available while still allowing the internal details of that field to be hidden Can also be static Do not require storage in memory Declared with accessors that enable you to control whether a value can be read or written and what should occur when doing so Get accessor enables the property value to be read Set accessor enables the value to be written

Properties class Person { private string firstName; // field public string FirstName // property get return this.firstName; } set this.firstName = value;

Properties The name of a property must be the same as the name of backing field, but starting with the upper-case letter The value keyword always means “the value that was provided by the caller” and is always typed to be the same as the property type None of the accessors is obligatory, however, at least one must be present Read-only property has only get accessor Write-only property has only set accessor (not reccomendable)

Calculated Properties You can create calculated properties that are read-only and do not have a backing field They are excellent ways to provide data derived from other information class Contact { private string firstName; private string lastName; public string FullName get return this.firstName + “ “ + this.lastName; }

Automatic Properties The simplest way to declare a property When using this syntax, you omit the backing field declaration and must always include both the get and set accessor without a declared implementation, which the compiler provides class Person { public string FirstName get; set; } This code will be transformed by the compiler to the code on the next slide

Automatic Properties class Person { private string firstName; public string FirstName get return this.firstName; } set this.firstName = value;

Encapsulation Encapsulation, as one of the principles of OOP, enables a class to hide the internal implementation details and itself from unwanted changes that would result in an invalid or inconsistent internal state Encapsulation is also sometimes referred to as data hiding Encapsulation is realized with help of properties

Methods If fields and properties define and implement data, methods, which are also called functions, define and implement a behavior or action that can be performed A method declaration can specify any access modifier A method can include the static modifier Methods can accept zero or more parameters, declared by the formal parameter list, which consists of one or more comma-separated parameters Each parameter must include both its type and an identifier If a method accepts no parameters, an empty parameter list must be specified

Methods The method signature is made up of the method name and the formal parameters list When two or more methods have the same name in a declaration space but have different method signatures, they are overloaded public int CalcAge() { return 2012 – yearOfBirth; }

Constructors A constructor is a special method which creates an instance of a class A constructor has the same name of the class It cannot return a value, which is different from a method that returns void If the constructor has no parameters, it is the default constructor Constructors are usually public

Declaring a Constructor public class Person { public Person() // default constructor } public Person(string firstName, string lastName) this.firstName = firstName; this.lastName = lastName;

Nested Classes A nested class is one that is fully enclosed, or nested, inside another class declaration Nested classes are a convenient way to allow an outer class to create and use objects without making them accessible outside of that class Are easy to overuse, making your class more difficult to work with Nested classes implicitly have at least the same access level as the containing class

Partial Classes Enable you to split the declaration of a class into multiple parts, typically across multiple files Are implemented in exactly the same way as normal classes but contain the keyword partial just before the class keyword When working with partial classes, all the parts must be available during compilation and have the same accessibility to form the complete class

Static Classes You can apply the static modifier to a class as well, which defines a static class A static class can have only a static constructor, and as a result, it is not possible to create an instance of a static class Static classes most commonly contain utility or helper methods that do not require a class instance to work Static classes can contain only static members. You must explicitly include the static modifier Any static member can be public, private, or internal

Object Initializers Person p1 = new Person(); p1.FirstName = “Jim”; p1.LastName = “Morrison”; Console.WriteLine(p1.ToString()); Person p2 = new Person { FirstName = “Jim”, LastName = “Morrison”, }; Console.WriteLine(p2.ToString());