Classes and OOP.

Slides:



Advertisements
Similar presentations
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
Advertisements

Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects reading: self-checks: Ch. 8 #1-9 exercises:
Road Map Introduction to object oriented programming. Classes
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
ACM/JETT Workshop - August 4-5, Object-Oriented Basics & Design.
Object Oriented Software Development
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 4 Objects and Classes.
Object Oriented Programming Lecturer: Andreas P. Adi
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Classes Methods and Properties. Introduction to Classes and Objects In object-oriented programming terminology, a class is defined as a kind of programmer-defined.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
1 Introduction to Object Oriented Programming Chapter 10.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Introduction to Object-oriented Programming
Object Oriented Programming
Classes (Part 1) Lecture 3
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING
Building Java Programs
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
CSE 143 Lecture 3 More ArrayList; object-oriented programming
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
suggested reading: Java Ch. 6
Can perform actions and provide communication
CS 106A, Lecture 22 More Classes
Object Based Programming
Inheritance Basics Programming with Inheritance
Chapter 3 Introduction to Classes, Objects Methods and Strings
C# Object Oriented Programming Concepts
Can perform actions and provide communication
Chapter 6 Methods: A Deeper Look
Encapsulation and Constructors
Dr. Bhargavi Dept of CS CHRIST
A programming problem Given a file of cities' (x, y) coordinates, which begins with the number of cities: Write.
Chapter 8 Lecture 8-1: Classes and Objects
Computer Programming with JAVA
Can perform actions and provide communication
CS2011 Introduction to Programming I Objects and Classes
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
Building Java Programs
COP 3330 Object-oriented Programming in C++
Fundaments of Game Design
Building Java Programs
Building Java Programs
A programming problem Given a file of cities' (x, y) coordinates, which begins with the number of cities: Write.
Introduction to Object-Oriented Programming
Building Java Programs
Building Java Programs
Object-Oriented PHP (1)
Building Java Programs
CPS120: Introduction to Computer Science
CSE 143 Lecture 3 More ArrayList; object-oriented programming
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
Chapter 9 Introduction To Classes
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 7 Objects and Classes
CSG2H3 Object Oriented Programming
Building Java Programs
Presentation transcript:

Classes and OOP

OOP The fundamental idea behind object-oriented programming is: The real world consists of objects. From the natural language definition of the word “class”: Collection of members that share certain attributes and functionality Likewise classes in object-oriented programming In object oriented programming languages (like C#, Java) classes are used to combine everything for a concept (e.g. date) Data (state / attributes) (e.g. date day, month, year) Methods (behavior / tasks) (e.g. display date, increment date)

Kinds of Objects Human Entities: employee, customer, worker, manager… Graphics program: point, line, circle… Data storage: linked list, stack, matrix… Car, Robot …

What is a class? Essentially a struct with built-in functions Properties and behaviours class Circle { double radius = 0.0; double Area() double pi = 3.141592; return (pi * radius * radius); } class keyword name of the class name of the method

Objects object: An entity that encapsulates data and behavior. data: variables inside the object behavior: methods inside the object You interact with the methods; the data is hidden in the object. Constructing (creating) an object: Type objectName = new Type(parameters); Calling an object's method: objectName.methodName(parameters);

Blackbox analogy creates iPod blueprint state: current song volume battery life behavior: power on/off change station/song change volume choose random song creates iPod #1 state: song = "1,000,000 Miles" volume = 17 battery life = 2.5 hrs behavior: power on/off change station/song change volume choose random song iPod #2 state: song = "Letting You" volume = 9 battery life = 3.41 hrs iPod #3 state: song = "Discipline" volume = 24 battery life = 1.8 hrs

Encapsulation By default the class definition encapsulates, or hides, the data inside it. Key concept of object oriented programming. Hiding data: why? you can drive cars, but you don’t need to know how the fuel injection works when the car’s fuel injection changes, you can still drive that new car The outside world can see and use the data only by calling the build-in functions. Called “methods” 8

Class Members Methods and variables declared inside a class are called members of that class. Member variables are called fields. Member functions are called methods. In order to be visible outside the class definition, a member must be declared public. As written in the previous example, neither the variable radius nor the method Area could be seen outside the class definition. 9

Making a Method Visible To make the Area() method visible outside we would write it as: public double Area() { return pi * radius * radius; } We will keep the radius and pi field private. 10

Methods The best way to develop and maintain a large application is to construct it from small, simple pieces  divide and conquer Methods allow you to modularize an application by separating its tasks into reusable units. Reuse the Framework Library, do not reinvent the wheel Divide your application into meaningful methods such that it is easier to debug and maintain. Methods == Worker analogy:

Methods and Classes The behavior of a class is defined by its methods and properties by which objects of that class are manipulated You should know about the public methods and what they do name of the method parameters and parameter types return type functionality You don’t need to know how the method is implemented or how the property is stored analogy: you can add two int variables using +, but you don’t need to know how computer really adds

First example Dice class Have a number of sides When rolled returns an int between{1- num_sides}

Another Class Example Lets write a class named Authenticator for password authentication as a console application It should have two member functions (i.e., methods) Check if given password is correct? Change a password

Access Modifiers private password; private variables/ functions are not accessible outside the class public variables/ functions allows access outside the class

Instantiating and Using Objects The class Authenticator contains everything the compiler needs to know about to process this new variable type. Constructor – create an instance Whenever a class object is created, its constructor is called A class may have multiple constructors that take different arguments There is always a default one Authenticator myAccess = new Authenticator();

Static variables Each instance of a class has its own set of fields. Authenticator alice = new Authenticator(); Authenticator bob = new Authenticator(); alice.ChangePassword("OldAlicePassword", “NewAlicePassword); bob.ChangePassword("OldBobPassword", "NewBobPassword); The two objects are different and have distinct values of password Variable that applies to all can be done static variable belongs to class not its instances private static uint minPasswordLength = 6;

GUI design for Authenticator class Lets design a windows form application similar to the one we did as console application

HW – account class Write a class for bank accounts A double field for balance A constructor Deposit and Withdraw methods Write a test program that generates at least two different accounts Apply withdraw and deposit functions and show they work correctly by printing the result.