Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 1 Dr Walid M. Aly 1 Lecture 4 Object- Oriented Programming (CS243) Group home page:

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Advertisements

The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Lecture 2: Object Oriented Programming I
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Information Hiding Chapter 5: Classes and Objects in Depth.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes Part 1.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes Part 1 Animated.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Introduction to methods Chapter 5: Classes and Objects in Depth.
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
Chapter Chapter 4 Defining Your Own Classes Part 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Review Java.
Develop Instantiable classes (continue). Exam 1 – section 02 Avg = 131 (87%). Max: 148.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Why Programmer-Defined Classes Using just the String,
Defining Your Own Classes Part 3. class { } Template for Class Definition Import Statements Class Comment Class Name Data Members Methods (incl. Constructor)
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Writing Classes (Chapter 4)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Chapter 10 METHODS AND CONSTRUCTORS 1. Accessing Objects  Referencing the object’s data: objectReference.data myCircle.radius  calling the object’s.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 4 Defining Your Own Classes Part 1 Animated Version.
BCS 2143 Object Oriented Programming Defining Your Own Classes (Part 1)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
DCS 2133 Object Oriented Programming Defining Your Own Classes Part 2.
Classes and Objects. Object Software objects are modeled after real-world objects in that they, too, have state and behavior. A software object maintains.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 4 Defining Your Own Classes Part 1 Animated Version.
 2005 Pearson Education, Inc. All rights reserved. 1 Classes and Objects: A Deeper Look.
Classes and Objects. How can one design a program?  Top-down structured design: uses algorithmic decomposition where each module denotes a major step.
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Classes and Objects. Object vs. Class Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2  A class could be considered as a set of objects having.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Dr. Majed Abdouli © Objects and Classes 1 Dr. Majed Abdouli © 2015, adapted from Liang, Introduction to Java Programming, Eighth Edition, (c) 2011.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 4 : Defining Your Own Classes Part 1 - Objectives After you have read and studied this chapter, you should be able to Define a class with multiple.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
GC211 Data structure Lecture 3 Sara Alhajjam.
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
CS1201: Programming Language 2
Defining Your Own Classes Part 1
Defining Your Own Classes
Chapter 5: Classes and Objects in Depth
Programs and Classes A program is made up from classes
Chapter 5: Classes and Objects in Depth
CS1201: Programming Language 2
Chapter 5: Classes and Objects in Depth
Presentation transcript:

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 1 Dr Walid M. Aly 1 Lecture 4 Object- Oriented Programming (CS243) Group home page: Defining your own classes

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 2 Why Programmer-Defined Classes? Using just standard API classes will not meet all of our needs. We need to be able to define our own classes customized for our applications. Learning how to define our own classes is the first step toward mastering the skills necessary in building large programs. Classes we define ourselves are called programmer-defined classes.

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 3 3 Banking System Class Employee Class Manager Class BankAccount Class CreditCard Student Registration System Class Student Class Lecturer Class Course Class ClassRoom Voting System Class Voting Class Citizen Class vote Class Candidate Library System Class Book Class LibraryMember Class Employee Example : classes in different systems

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 4 4 class className { } class className { } Variables describing state Methods describing behavior class Student { String name; double GPA; Int regidterationNumber; ………………….. void registerUnit() { ………………… } void payFees(){……} } class Student { String name; double GPA; Int regidterationNumber; ………………….. void registerUnit() { ………………… } void payFees(){……} } Student object 1 name =Ahmed Aly registration number= GPA=3.2 register a unit Student object 2 name =Mohamed Fathi registration number=34345 GPA=2.2 payFees

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 5 Real-world objects share two characteristics: they all have state and behavior. in your program, you will have software objects which are objects that you want to manipulate. The state of an object is represented by variables with their current values. The behavior of an object is defined by a set of methods The terms object and instance are interchangeable. Objects of the same type are defined using a common class. The class defines the type of variables the object can have and the type of method the object can execute. 5 Objects & classes Radio might have states (on, off, current volume, current station) and behavior (turn on, turn off, increase volume, decrease volume, seek, scan, and tune). Radio

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 6 6 Example :Class Radio public class Radio{ int volume; String station; boolean powerStatus; public void increaseVolume(){ volume++; } public void decreaseVolume(){ volume--; } public void changeStation(String newStation){ station=newStation; } public void turnOn(){ volume=3; powerStatus=true; } public void turnOff(){ volume=0; powerStatus=false; } } volume =4 station=“Negoom FM” Powerstatus=true Radio object1 volume =7 station=“Quran” Powerstatus=true Radio object2 volume =0 station=“FM” Powerstatus=false Radio object3 volume =3 station=“FM” Powerstatus=true Turn On

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 7 class { } Template for Class Definition Import Statements Class Comment Class Name Data Members Methods (incl. Constructor) Methods (incl. Constructor) A class will have variables, constructors and methods. All these are known as class members

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 8 The Definition of the Bicycle Class class Bicycle { // Data Member private String ownerName; //Constructor: Initialzes the data member public Bicycle( ) { ownerName = "Unknown"; } //Returns the name of this bicycle's owner public String getOwnerName( ) { return ownerName; } //Assigns the name of this bicycle's owner public void setOwnerName(String name) { ownerName = name; }

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 9 Chapter Data Member Declaration ; private String ownerName ; Modifiers Data Type Name Note: There’s only one modifier in this example.  Class members can be declared with access modifier as public or protected or private.  the default access modifier is “ package”

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 10 Chapter Method Declaration ( ){ } public void setOwnerName ( String name ) { ownerName = name; } Statements Modifier Return Type Method Name Parameter  Class members can be declared with access modifier as public or protected or private.  the default access modifier is “ package”

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 11 Constructor A constructor is a special method that is executed when a new instance of the class is created. public ( ){ } public Bicycle ( ) { ownerName = "Unassigned"; } Statements Modifier Class Name Parameter

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 12 Constructors….. 12  Constructor must have the same name as the class name.  Constructors do not have a return type—not even void, can not be static.  default blank constructor, is provided automatically only if no constructors are explicitly declared in the class.  A constructor is generally used to do initial setup for an object class Circle { double radius=0 ; public Circle() { } public Circle (double r) { radius=r; } public double findArea() { // } class Circle { double radius=0 ; public Circle() { } public Circle (double r) { radius=r; } public double findArea() { // } multiple constructors with the same name but different signatures can exist.(overloading)

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 13 class Circle { double radius=0 ; public Circle() { } public Circle (double r) { radius=r; } public double findArea() { return radius * radius * 3.14; } class Circle { double radius=0 ; public Circle() { } public Circle (double r) { radius=r; } public double findArea() { return radius * radius * 3.14; } } class Test{ public static void main (String [] arg) { Circle c1; c1=new Circle(); Circle c2= new Circle(); Circle c3= new Circle(5.1); } class Test{ public static void main (String [] arg) { Circle c1; c1=new Circle(); Circle c2= new Circle(); Circle c3= new Circle(5.1); } How to create an object from class?  To construct an object from a class, invoke a constructor of the class using the new operator, as follows: new ClassName(arguments);  constructor can contain any type of code that a normal method could contain. When constructor is invoked an object is created and also code in constructor is executed. radius=0 radius=5.1 c1 c2c3 c1,c2,c3 are reference data types

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 14 Revisiting the Bicycle Class class Bicycle { // Data Member private String ownerName; //Constructor: Initialzes the data member public Bicycle( ) { ownerName = "Unknown"; } //Returns the name of this bicycle's owner public String getOwnerName( ) { return ownerName; } //Assigns the name of this bicycle's owner public void setOwnerName(String name) { ownerName = name; }

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 15 Using the Bicycle Class class BicycleRegistration { public static void main(String[] args) { Bicycle bike1, bike2; String owner1, owner2; bike1 = new Bicycle( ); //Create and assign values to bike1 bike1.setOwnerName("Adam Smith"); bike2 = new Bicycle( ); //Create and assign values to bike2 bike2.setOwnerName("Ben Jones"); owner1 = bike1.getOwnerName( ); //Output the information owner2 = bike2.getOwnerName( ); System.out.println(owner1 + " owns a bicycle."); System.out.println(owner2 + " also owns a bicycle."); }

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 16 Chapter Multiple Instances Once the Bicycle class is defined, we can create multiple instances. Bicycle bike1, bike2; bike1bike2 : Bicycle ownerName : Bicycle ownerName “Adam Smith” “Ben Jones” bike1 = new Bicycle( ); bike1.setOwnerName("Adam Smith"); bike2 = new Bicycle( ); bike2.setOwnerName("Ben Jones"); Sample Code

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 17 Example :Adding constructors to class Radio 17 public class Radio{ int volume; String station; boolean powerStatus; public Radio() { volume=3; } public Radio(int startVolume) { volume= startVolume; } public void increaseVolume(){ volume++; } public void decreaseVolume(){ volume--; } public void changeStation(String newStation){ station=newStation;} public void turnOn(){ powerStatus=true; } public void turnOff(){ volume=0; powerStatus=false; } class Test { public static void main (String [] args) { Radio radio1=new Radio(); radio1.turnOn(); radio1.increaseVolume(); Radio radio2=new Radio(5); }

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 18 public class Box { double width; double height; double depth; double volume; public Box(double w,double h,double d) { width=w; height=h; depth=d; } public class Test { public static void main(String args[]) { Box mybox1 = new Box( ); } 1-compile error 2-Runtime error 3-Compile and run without error What will be the output ? 1-compile error Default constructor should be written

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 19 Example :The Account Class class Account { private String ownerName; private double balance; public Account( ) { ownerName = "Unassigned"; balance = 0.0; } public void add(double amt) { balance = balance + amt; } public void deduct(double amt) { balance = balance - amt; } public double getCurrentBalance( ) { return balance; } public String getOwnerName( ) { return ownerName; } public void setInitialBalance (double bal) { balance = bal; } public void setOwnerName (String name) { ownerName = name; } Page 1Page 2

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 20 Arguments and Parameters An argument is a value we pass to a method class Account {... public void add(double amt) { balance = balance + amt; }... } class Sample { public static void main(String[] arg) { Account acct = new Account();... acct.add(400);... }... } argument parameter A parameter is a placeholder in the called method to hold the value of the passed argument.

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 21 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Matching Arguments and Parameters The number or arguments and the parameters must be the same class Demo { public void compute(int i, int j, double x) {... } Demo demo = new Demo( ); int i = 5; int k = 14; demo.compute(i, k, 20); 3 arguments 3 parameters The matched pair must be assignment- compatible (e.g. you cannot pass a double argument to a int parameter) Arguments and parameters are paired left to right Passing Side Receiving Side

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 22 Calling a constructor from another constructor 22 tthis is used to call a constructor from another constructor. ccall to this must be first statement in constructor otherwise compile error class Box{ double width; double height; double depth; Box(double w, double h, double d){ width = w; height = h; depth = d; } Box() { width = 1; height = 1; depth = 1; } Box(double y) { this(y,y,y); } }

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 23 Information Hiding and Visibility Modifiers The modifiers public and private designate the accessibility of data members and methods. If a class component (data member or method) is declared private, client classes cannot access it. If a class component is declared public, client classes can access it. Internal details of a class are declared private and hidden from the clients. This is information hiding.

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 24 Chapter Accessibility Example class Service { public int memberOne; private int memberTwo; public void doOne() { … } private void doTwo() { … } … Service obj = new Service(); obj.memberOne = 10; obj.memberTwo = 20; obj.doOne(); obj.doTwo(); … ClientService

Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 25 Data Members Should Be private Data members are the implementation details of the class, so they should be invisible to the clients. Declare them private. Exception: Constants can (should) be declared public if they are meant to be used directly by the outside methods.