UFCE3T-15-M Object-oriented Design and Programming

Size: px
Start display at page:

Download "UFCE3T-15-M Object-oriented Design and Programming"

Transcription

1 UFCE3T-15-M Object-oriented Design and Programming Block1: Objects and Classes Jin Sa 27-Sep-05 UFCE3T-15-M Programming part 1

2 Objectives To understand objects and classes and use classes to model objects. To learn how to declare a class and how to create an object of a class. To understand the roles of constructors and use constructors to create objects. To distinguish between object reference variables and primitive data type variables. To use classes in the Java library. To declare private data field with appropriate get and set methods. 27-Sep-05 UFCE3T-15-M Programming part 2

3 Objectives To develop methods with object arguments. To understand the difference between instance and static variables and methods. To use the keyword this as the reference to the current object that invokes the instance method. To store and process objects in arrays. To understand the use of various visibility modifiers. To apply class abstraction to develop software. 27-Sep-05 UFCE3T-15-M Programming part 3

4 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly identified, e.g. a student, a desk, a circle. An object has a unique identity, state, and behaviors. The state of an object consists of a set of data fields with their current values. The behavior of an object is defined by a set of methods. 27-Sep-05 UFCE3T-15-M Programming part 4

5 Classes Classes are constructs that define objects of the same type. A Java class uses variables to define data fields and methods to define behaviors. Constructors are special methods, which are invoked to construct objects from the class. A class is a template that defines what an object s data and methods will be. An object is an instance of a class. 27-Sep-05 UFCE3T-15-M Programming part 5

6 Classes class Circle { /** The radius of this circle */ double radius = 1.0; /** Construct a circle object */ Circle() { /** Construct a circle object */ Circle(double newradius) { radius = newradius; Data field/ attributes More than one constructors /** Return the area of this circle */ double findarea() { return radius * radius * ; Method 27-Sep-05 UFCE3T-15-M Programming part 6

7 Circle() { Constructors Circle(double newradius) { radius = newradius; Constructors are a special kind of methods that are invoked to construct objects. 27-Sep-05 UFCE3T-15-M Programming part 7

8 Constructors, cont. A constructor with no parameters is referred to as a no-arg constructor. Constructors must have the same name as the class itself. Constructors do not have a return type not even void. Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects. 27-Sep-05 UFCE3T-15-M Programming part 8

9 Creating Objects Using new ClassName(); Constructors Example: new Circle(); new Circle(5.0); 27-Sep-05 UFCE3T-15-M Programming part 9

10 Default Constructor A class may be declared without constructors. In this case, a no-arg constructor with an empty body is implicitly declared in the class. This constructor, called a default constructor, is provided automatically only if no constructors are explicitly declared in the class. 27-Sep-05 UFCE3T-15-M Programming part 10

11 Declaring Object Reference Variables To reference an object, assign the object to a reference variable. To declare a reference variable, use the syntax: ClassName objectrefvar; Example: Circle mycircle; 27-Sep-05 UFCE3T-15-M Programming part 11

12 Declaring/Creating Objects in a Single Step ClassName objectrefvar = new ClassName(); Assign object reference Create an object Example: Circle mycircle = new Circle(); 27-Sep-05 UFCE3T-15-M Programming part 12

13 Accessing Objects Referencing the object s data: objectrefvar.data e.g., mycircle.radius Invoking the object s method: objectrefvar.methodname(arguments) e.g., mycircle.findarea() 27-Sep-05 UFCE3T-15-M Programming part 13

14 Example: Using Objects Objective: Demonstrate creating objects, accessing data, and using methods. (run../sourcecode/codeblock1/testsimplecircle.java) 27-Sep-05 UFCE3T-15-M Programming part 14

15 // SimpleCircle class has two constructors class SimpleCircle { double radius; /** Construct a circle with radius 1 */ SimpleCircle() { radius = 1.0; /** Construct a circle with a specified radius */ SimpleCircle(double newradius) { radius = newradius; /** Return the area of this circle */ double findarea() { return radius * radius * 3.14; 27-Sep-05 UFCE3T-15-M Programming part 15

16 public class TestSimpleCircle { /** Main method */ public static void main(string[] args) { // Create a circle with radius 5.0 SimpleCircle mycircle = new SimpleCircle(5.0); System.out.println("The area for radius" + mycircle.radius + " is "+mycircle.findarea()); // Create a circle with radius 1 SimpleCircle yourcircle = new SimpleCircle(); System.out.println(...); // Modify circle radius yourcircle.radius = 100; System.out.println(...); 27-Sep-05 UFCE3T-15-M Programming part 16

17 Differences between Variables of rimitive Data Types and Object Types Primitive type int i = 1 i 1 Created using new Circle() Object type Circle c c reference c: Circle radius = 1 27-Sep-05 UFCE3T-15-M Programming part 17

18 Copying Variables of Primitive Data Types and Object Types Primitive type assignment i = j Object type assignment c1 = c2 Before: After: Before: After: i 1 i 2 c1 c1 j 2 j 2 c2 c2 c1: Circle c2: Circle radius = 5 radius = 9 27-Sep-05 UFCE3T-15-M Programming part 18

19 Using Classes from the Java Library We declared the Circle class and created objects from the class. Often you will use the classes in the Java library to develop programs. 27-Sep-05 UFCE3T-15-M Programming part 19

20 Use of Math class in Java.lang class SimpleCircle { double radius;... /** Return the area of this circle */ double findarea() { return (radius * radius * Math.PI); 27-Sep-05 UFCE3T-15-M Programming part 20

21 The Date Class Java provides a system-independent encapsulation of date and time in the java.util.date class. You can use the Date class to create an instance for the current date and time and use its tostring method to return the date and time as a string. 27-Sep-05 UFCE3T-15-M Programming part 21

22 Use the Date class For example, the following code java.util.date date = new java.util.date(); System.out.println(date.toString()); displays a string like Tue Sep 27 13:50:19 EST Sep-05 UFCE3T-15-M Programming part 22

23 Visibility Modifiers public The class, data, or method is visible to any class in any package. private The data or methods can be accessed only by the declaring class. By default, the class, variable, or method can accessed by any class in the same package. 27-Sep-05 UFCE3T-15-M Programming part 23

24 package p1; public class C1 { public int x; int y; private int z; public void m1() { void m2() { private void m3() { public class C2 { C1 o = new C1(); can access o.x; can access o.y; cannot access o.z; can invoke o.m1(); can invoke o.m2(); cannot invoke o.m3(); package p2; public class C3 { C1 o = new C1(); can access o.x; cannot access o.y; cannot access o.z; can invoke o.m1(); cannot invoke o.m2(); cannot invoke o.m3(); The private modifier restricts access to within an object, the default modifier restricts access to within a package, and the public modifier enables unrestricted access. 27-Sep-05 UFCE3T-15-M Programming part 24

25 Why Data Fields Should Be private? To protect data. To make class easy to maintain. 27-Sep-05 UFCE3T-15-M Programming part 25

26 Example of Data Field Encapsulation In this example, private data are used for the radius and the accessor (or get and set) methods getradius and setradius are provided for the clients to retrieve and modify the radius. 27-Sep-05 UFCE3T-15-M Programming part 26

27 public class Circle { private double radius; // private attribute... /** Use a get method to return radius */ public double getradius() { return radius; /** Use a set method to set a new radius */ public void setradius(double newradius) { radius = (newradius >= 0)? newradius : 0; Sep-05 UFCE3T-15-M Programming part 27

28 // TestCircle.java: Demonstrate private modifier public class TestCircle { /** Main method */ public static void main(string[] args) { Circle mycircle = new Circle(5.0); System.out.println("The area for radius " + mycircle.getradius() + " is " + mycircle.findarea()); // Double mycircle's radius mycircle.setradius(mycircle.getradius() * 2); System.out.println("The area radius " + mycircle.getradius() + " is " + mycircle.findarea()); 27-Sep-05 UFCE3T-15-M Programming part 28

29 Passing Objects to Methods Passing by value for primitive type value (the value is passed to the parameter) Passing by value for reference type value (the value is the reference to the object) 27-Sep-05 UFCE3T-15-M Programming part 29

30 public class TestPassObject { /** Print a table of areas for radius */ public void printareasupto(circle c, int times) { System.out.println("Radius \t\tarea"); for (int i=0; i<times; i++) { System.out.println(c.getRadius() + "\t\t\t" + c.findarea()); c.setradius(c.getradius() + 1); // end of printareaupto 27-Sep-05 UFCE3T-15-M Programming part 30

31 /** Main method */ public static void main(string[] args) { // Create a Circle object with radius 1 Circle mycircle = new Circle(); TestPassObject mypassobject = new TestPassObject(); //Print areas for radius 1, 2, 3, 4, 5. mypassobject.printareasupto(mycircle, 5); 27-Sep-05 UFCE3T-15-M Programming part 31

32 Instance Variables, and Methods Instance variables belong to a specific instance. Instance methods are invoked by an instance of the class. 27-Sep-05 UFCE3T-15-M Programming part 32

33 Static (Class) Variables, Constants, and Methods Static variables are shared by all the instances of the class. Static methods are not tied to a specific object. Static constants are final variables shared by all the instances of the class. 27-Sep-05 UFCE3T-15-M Programming part 33

34 Static Variables, Constants, and Methods, cont. To declare static variables, constants, and methods, use the static modifier. 27-Sep-05 UFCE3T-15-M Programming part 34

35 Example of Using Instance and Class Variables and Method Objective: Demonstrate the roles of instance and class variables and their uses. This example adds a class variable numofobjects to track the number of Circle objects created. 27-Sep-05 UFCE3T-15-M Programming part 35

36 public class CircleStatic { private double radius; /** The number of the objects created */ private static int numberofobjects = 0; /** Construct a circle with radius 1 */ public CircleStatic() { radius = 1.0; numberofobjects++; public CircleStatic(double newradius) { radius = newradius; numberofobjects++; 27-Sep-05 UFCE3T-15-M Programming part 36

37 ... /** Set a new radius */ public void setradius(double newradius) { radius = newradius; /** Return numberofobjects */ public static int getnumberofobjects() { return numberofobjects; public double findarea() { return radius * radius * Math.PI; 27-Sep-05 UFCE3T-15-M Programming part 37

38 public class TestCircleStatic { /** Main method */ public static void main(string[] args) { CircleStatic circle1 = new CircleStatic(); System.out.println("Circle1 is radius: " + circle1.getradius() + "and number of Circle objects:" + CircleStatic.getNumberOfObjects()); CircleStatic circle2 = new CircleStatic(5); System.out.println("Circle2 is radius: " + circle2.getradius() + "and number of Circle objects:" + CircleStatic.getNumberOfObjects()); 27-Sep-05 UFCE3T-15-M Programming part 38

39 The this Keyword Use this to refer to the object that invokes the instance method. Use this to refer to an instance data field. 27-Sep-05 UFCE3T-15-M Programming part 39

40 Example of using this lass Foo { int i = 5; static double k = 0; void seti(int i) { this.i = i; Suppose that f1 and f2 are two objects of Foo Invoking f1.seti(10) is to execute f1.i = 10, where this is replaced by f1 Invoking f2.seti(45) is to execute f2.i = 45, where this is replaced by f2 27-Sep-05 UFCE3T-15-M Programming part 40

41 Example of using this public class Circle { private double radius; public Circle(double radius) { this.radius = radius; public Circle() { this(1.0); this must be explicitly used to reference the data field radius of the object being constructed this is used to invoke another constructor public double findarea() { return radius * radius * Math.PI; 27-Sep-05 UFCE3T-15-M Programming part 41

42 Array of Objects Circle[] circlearray = new Circle[10]; An array of objects is actually an array of reference variables. circlearray[1].findarea() involves two levels of referencing. circlearray references to the entire array. circlearray[1] references to a Circle object. 27-Sep-05 UFCE3T-15-M Programming part 42

43 Array of Objects, cont. Circle[] circlearray = new Circle[10]; circlearray reference circlearray[0] Circle object 0 circlearray[1] Circle object 1 circlearray[9] Circle object 9 27-Sep-05 UFCE3T-15-M Programming part 43

44 Array of Objects, cont. Example: add up the areas of the circles 27-Sep-05 UFCE3T-15-M Programming part 44

45 public class ManyCircles { Circle[] circlearray; public void createcirclearray() { circlearray = new Circle[10]; for (int i = 0; i < circlearray.length; i++) { circlearray[i] = new Circle(Math.random() * 100); public void printcirclearray() { System.out.println("Radius\t\t\t\t" + "Area"); for (int i = 0; i < circlearray.length; i++) { System.out.print(circleArray[i].getRadius() + "\t\t" + circlearray[i].findarea() + '\n'); 27-Sep-05 UFCE3T-15-M Programming part 45

46 /** Add circle areas */ public void sumarray() { // Initialize sum double sum = 0; // Add areas to sum for (int i=0; i<circlearray.length; i++) sum += circlearray[i].findarea(); System.out.println( "The area of all the circles: "+ sum); 27-Sep-05 UFCE3T-15-M Programming part 46

47 /** Main method */ public static void main(string[] args) { ManyCircles allcircles = new ManyCircles(); // Create circlearray allcircles.createcirclearray(); // Print circlearray allcircles.printcirclearray(); // Print total areas of the circles allcircles.sumarray(); 27-Sep-05 UFCE3T-15-M Programming part 47

48 Class Abstraction and Encapsulation Class abstraction means to separate class implementation from the use of the class. The creator of the class provides a description of the class and let the user know how the class can be used. The user of the class does not need to know how the class is implemented. The detail of implementation is encapsulated and hidden from the user using the visibility modifiers. 27-Sep-05 UFCE3T-15-M Programming part 48

Chapter 8 Objects and Classes

Chapter 8 Objects and Classes Chapter 8 Objects and Classes 1 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections, loops, methods, and arrays. However, these Java

More information

Chapter 9. Objects and Classes

Chapter 9. Objects and Classes Chapter 9 Objects and Classes 1 OO Programming in Java Other than primitive data types (byte, short, int, long, float, double, char, boolean), everything else in Java is of type object. Objects we already

More information

Chapter 8 Objects and Classes. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Chapter 8 Objects and Classes. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Chapter 8 Objects and Classes 1 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections, loops, methods, and arrays. However, these Java

More information

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. Chapter 9 Objects and Classes rights reserved. 1 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections, loops, methods, and arrays. However,

More information

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. Chapter 9 Objects and Classes 1 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections, loops, methods, and arrays. However, these Java

More information

OO Programming Concepts

OO Programming Concepts Chapter 8 Objects and Classes 1 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections, loops, methods, and arrays. However, these Java

More information

ECOM 2324 COMPUTER PROGRAMMING II

ECOM 2324 COMPUTER PROGRAMMING II ECOM 2324 COMPUTER PROGRAMMING II Object Oriented Programming with JAVA Instructor: Ruba A. Salamh Islamic University of Gaza 2 CHAPTER 9 OBJECTS AND CLASSES Motivations 3 After learning the preceding

More information

Chapter 9 Objects and Classes. OO Programming Concepts. Classes. Objects. Motivations. Objectives. CS1: Java Programming Colorado State University

Chapter 9 Objects and Classes. OO Programming Concepts. Classes. Objects. Motivations. Objectives. CS1: Java Programming Colorado State University Chapter 9 Objects and Classes CS1: Java Programming Colorado State University Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections, loops,

More information

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs Chapter 8 User-Defined Classes and ADTs Objectives To understand objects and classes and use classes to model objects To learn how to declare a class and how to create an object of a class To understand

More information

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Chapter 8 Objects and Classes Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk 1 Motivations After learning the preceding chapters,

More information

Chapter 3 Objects and Classes

Chapter 3 Objects and Classes Chapter 3 Objects and Classes Topics OO Programming Concepts Creating Objects and Object Reference Variables Constructors Modifiers Instance and Class Variables and Methods Scope of Variables OO Programming

More information

Chapter 8 Objects and Classes Part 1

Chapter 8 Objects and Classes Part 1 Chapter 8 Objects and Classes Part 1 1 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly

More information

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Objects and Classes (contd.) Course Lecture Slides 19 May 2010 Ganesh Viswanathan Objects and Classes Credits: Adapted from CIS3023 lecture

More information

OOP Part 2. Introduction to OOP with Java. Lecture 08: Introduction to OOP with Java - AKF Sep AbuKhleiF -

OOP Part 2. Introduction to OOP with Java. Lecture 08: Introduction to OOP with Java - AKF Sep AbuKhleiF - Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017 Lecture 08: OOP Part 2 Instructor: AbuKhleif, Mohammad Noor Sep 2017 AbuKhleiF - 1 Instructor AbuKhleif, Mohammad Noor Computer

More information

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017 Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017 Lecture 07: OOP Part 1 Instructor: AbuKhleif, Mohammad Noor Sep 2017 Instructor AbuKhleif, Mohammad Noor Computer Engineer (JU

More information

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9 Chapter 6 Arrays Chapter 7 Objects and Classes Chapter 8 Strings and Text I/O Chapter 9 Inheritance and Polymorphism GUI can be covered after 10.2, Abstract Classes Chapter 12 GUI Basics 10.2, Abstract

More information

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. Chapter 9 Objects and Classes 1 Objectives Classes & Objects ( 9.2). UML ( 9.2). Constructors ( 9.3). How to declare a class & create an object ( 9.4). Separate a class declaration from a class implementation

More information

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9 Chapter 7 Objects and Classes Chapter 6 Arrays Chapter 7 Objects and Classes Chapter 8 Strings and Text I/O Chapter 9 Inheritance and Polymorphism GUI can be covered after 10.2, Abstract Classes Chapter

More information

CS 170, Section /3/2009 CS170, Section 000, Fall

CS 170, Section /3/2009 CS170, Section 000, Fall Lecture 18: Objects CS 170, Section 000 3 November 2009 11/3/2009 CS170, Section 000, Fall 2009 1 Lecture Plan Homework 5 : questions, comments? Managing g g Data: objects to make your life easier ArrayList:

More information

Programming in the Large II: Objects and Classes (Part 1)

Programming in the Large II: Objects and Classes (Part 1) Programming in the Large II: Objects and Classes (Part 1) 188230 Advanced Computer Programming Asst. Prof. Dr. Kanda Runapongsa Saikaew (krunapon@kku.ac.th) Department of Computer Engineering Khon Kaen

More information

2 What are the differences between constructors and methods?

2 What are the differences between constructors and methods? 1 Describe the relationship between an object and its defining class. How do you define a class? How do you declare an object reference variable? How do you create an object? How do you declare and create

More information

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects Islamic University of Gaza Faculty of Engineering Computer Engineering Dept Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn how to describe objects and classes and how to define

More information

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber Chapter 10 Inheritance and Polymorphism Dr. Hikmat Jaber 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the

More information

Chapter 8 Objects and Classes Dr. Essam Halim Date: Page 1

Chapter 8 Objects and Classes Dr. Essam Halim Date: Page 1 Assignment (1) Chapter 8 Objects and Classes Dr. Essam Halim Date: 18-3-2014 Page 1 Section 8.2 Defining Classes for Objects 1 represents an entity in the real world that can be distinctly identified.

More information

Object-Oriented Programming Concepts

Object-Oriented Programming Concepts Object-Oriented Programming Concepts Real world objects include things like your car, TV etc. These objects share two characteristics: they all have state and they all have behavior. Software objects are

More information

Classes and Objects. CGS 3416 Spring 2018

Classes and Objects. CGS 3416 Spring 2018 Classes and Objects CGS 3416 Spring 2018 Classes and Objects An object is an encapsulation of data along with functions that act upon that data. It attempts to mirror the real world, where objects have

More information

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes Based on Introduction to Java Programming, Y. Daniel Liang, Brief Version, 10/E 1 Creating Classes and Objects Classes give us a way of defining custom data types and associating data with operations on

More information

Chapter 8 Objects and Classes

Chapter 8 Objects and Classes Chapter 8 Objects and Classes 1 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections, loops, methods, and arrays. However, these Java

More information

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay Classes - 2 Data Processing Course, I. Hrivnacova, IPN Orsay OOP, Classes Reminder Requirements for a Class Class Development Constructor Access Control Modifiers Getters, Setters Keyword this const Member

More information

CH. 2 OBJECT-ORIENTED PROGRAMMING

CH. 2 OBJECT-ORIENTED PROGRAMMING CH. 2 OBJECT-ORIENTED PROGRAMMING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) OBJECT-ORIENTED

More information

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java. Classes Introduce to classes and objects in Java. Classes and Objects in Java Understand how some of the OO concepts learnt so far are supported in Java. Understand important features in Java classes.

More information

Last lecture. Lecture 9. in a nutshell. in a nutshell 2. Example of encapsulation. Example of encapsulation. Class test. Procedural Programming

Last lecture. Lecture 9. in a nutshell. in a nutshell 2. Example of encapsulation. Example of encapsulation. Class test. Procedural Programming 1 Lecture 9 Last lecture Class test Has been marked Collect your marks at your next seminar Seminars There are seminars this week to go through the class test Meet in the classroom indicated on the timetable

More information

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

More information

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Lecture Notes Chapter #9_b Inheritance & Polymorphism Lecture Notes Chapter #9_b Inheritance & Polymorphism Inheritance results from deriving new classes from existing classes Root Class all java classes are derived from the java.lang.object class GeometricObject1

More information

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

IST311. Advanced Issues in OOP: Inheritance and Polymorphism IST311 Advanced Issues in OOP: Inheritance and Polymorphism IST311/602 Cleveland State University Prof. Victor Matos Adapted from: Introduction to Java Programming: Comprehensive Version, Eighth Edition

More information

Object Oriented Programming SCJ2153. Class and Object. Associate Prof. Dr. Norazah Yusof

Object Oriented Programming SCJ2153. Class and Object. Associate Prof. Dr. Norazah Yusof Object Oriented Programming SCJ2153 Class and Object Associate Prof. Dr. Norazah Yusof Classes Java program consists of classes. Class is a template for creating objects. Class normally consists of 3 components:

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Inheritance and Polymorphism Dr. M. G. Abbas Malik Assistant Professor Faculty of Computing and IT (North Jeddah Branch) King Abdulaziz University, Jeddah, KSA mgmalik@kau.edu.sa www.sanlp.org/malik/cpit305/ap.html

More information

Making New instances of Classes

Making New instances of Classes Making New instances of Classes NOTE: revised from previous version of Lecture04 New Operator Classes are user defined datatypes in OOP languages How do we make instances of these new datatypes? Using

More information

Inheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University

Inheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University Inheritance and Polymorphism CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation Model classes with similar properties and methods: Circles, rectangles

More information

Object Oriented System Development Paradigm. Sunnie Chung CIS433 System Analysis Methods

Object Oriented System Development Paradigm. Sunnie Chung CIS433 System Analysis Methods Object Oriented System Development Paradigm Sunnie Chung CIS433 System Analysis Methods OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents

More information

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles, Chapter 11 Inheritance and Polymorphism 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design

More information

Object-Oriented Programming Concepts

Object-Oriented Programming Concepts Object-Oriented Programming Concepts Object-oriented programming מונחה עצמים) (תכנות involves programming using objects An object ) represents (עצם an entity in the real world that can be distinctly identified

More information

Lecture 6 Introduction to Objects and Classes

Lecture 6 Introduction to Objects and Classes Lecture 6 Introduction to Objects and Classes Outline Basic concepts Recap Computer programs Programming languages Programming paradigms Object oriented paradigm-objects and classes in Java Constructors

More information

Object Oriented Programming in C#

Object Oriented Programming in C# Introduction to Object Oriented Programming in C# Class and Object 1 You will be able to: Objectives 1. Write a simple class definition in C#. 2. Control access to the methods and data in a class. 3. Create

More information

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1.

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1. Overview Inf1-OOP Creating Data Types 1 Circle Class Object Default Perdita Stevens, adapting earlier version by Ewan Klein Format Strings School of Informatics January 11, 2015 HotelRoom Class More on

More information

141214 20219031 1 Object-Oriented Programming concepts Object-oriented programming ( תכנות מונחה עצמים ) involves programming using objects An object ) (עצם represents an entity in the real world that

More information

What is Inheritance?

What is Inheritance? Inheritance 1 Agenda What is and Why Inheritance? How to derive a sub-class? Object class Constructor calling chain super keyword Overriding methods (most important) Hiding methods Hiding fields Type casting

More information

Chapter 5 Programming with Objects and Classes

Chapter 5 Programming with Objects and Classes Chapter 5 Programming with Objects and Classes OO Programming Concepts Declaring and Creating Objects Constructors Modifiers (public, private and static) Instance and Class Variables and Methods Scope

More information

Chapter 4. Defining Classes I

Chapter 4. Defining Classes I Chapter 4 Defining Classes I Introduction Classes are the most important language feature that make object-oriented programming (OOP) possible Programming in Java consists of defining a number of classes

More information

Chapter 11 Inheritance and Polymorphism

Chapter 11 Inheritance and Polymorphism Chapter 11 Inheritance and Polymorphism 1 Motivations OOP is built on three principles: Encapsulation (classes/objects, discussed in chapters 9 and 10), Inheritance, and Polymorphism. Inheritance: Suppose

More information

JAVA CLASSES AND OBJECTS

JAVA CLASSES AND OBJECTS SE2205B - DATA STRUCTURES AND ALGORITHMS JAVA CLASSES AND OBJECTS Kevin Brightwell Thursday January 12th, 2017 Acknowledgements:Dr. Quazi Rahman 1 / 31 LECTURE OUTLINE Course Updates Arrays Objects and

More information

COMP 250 Winter 2011 Reading: Java background January 5, 2011

COMP 250 Winter 2011 Reading: Java background January 5, 2011 Almost all of you have taken COMP 202 or equivalent, so I am assuming that you are familiar with the basic techniques and definitions of Java covered in that course. Those of you who have not taken a COMP

More information

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Constructor Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design these classes so to avoid redundancy? The

More information

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017 Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017 Lecture 11: Inheritance and Polymorphism Part 1 Instructor: AbuKhleif, Mohammad Noor Sep 2017 Instructor AbuKhleif, Mohammad

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed 1 1 COMP200 INHERITANCE OOP using Java, from slides by Shayan Javed 2 Inheritance Derive new classes (subclass) from existing ones (superclass). Only the Object class (java.lang) has no superclass Every

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract

More information

JAVA: A Primer. By: Amrita Rajagopal

JAVA: A Primer. By: Amrita Rajagopal JAVA: A Primer By: Amrita Rajagopal 1 Some facts about JAVA JAVA is an Object Oriented Programming language (OOP) Everything in Java is an object application-- a Java program that executes independently

More information

Computer Science II (20073) Week 1: Review and Inheritance

Computer Science II (20073) Week 1: Review and Inheritance Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system

More information

Questions Answer Key Questions Answer Key Questions Answer Key

Questions Answer Key Questions Answer Key Questions Answer Key Benha University Term: 2 nd (2013/2014) Class: 2 nd Year Students Subject: Object Oriented Programming Faculty of Computers & Informatics Date: 26/4/2014 Time: 1 hours Exam: Mid-Term (A) Name:. Status:

More information

CLASSES AND OBJECTS. Fundamentals of Computer Science I

CLASSES AND OBJECTS. Fundamentals of Computer Science I CLASSES AND OBJECTS Fundamentals of Computer Science I Outline Primitive types Creating your own data types Classes Objects Instance variables Instance methods Constructors Arrays of objects A Foundation

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract

More information

Object Oriented Programming COP3330 / CGS5409

Object Oriented Programming COP3330 / CGS5409 Object Oriented Programming COP3330 / CGS5409 Classes & Objects DDU Design Constructors Member Functions & Data Friends and member functions Const modifier Destructors Object -- an encapsulation of data

More information

Object Oriented Programming

Object Oriented Programming Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Lab 11 Object Oriented Programming Eng. Mohammed Alokshiya December 16, 2014 Object-oriented

More information

Inf1-OP. Creating Classes. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

Inf1-OP. Creating Classes. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics Inf1-OP Creating Classes Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics February 26, 2018 Creating classes Last time we saw how to use a class: create a

More information

Class Foo. instance variables. instance methods. Class FooTester. main {

Class Foo. instance variables. instance methods. Class FooTester. main { Creating classes Inf1-OP Creating Classes Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics February 26, 2018 Last time we saw how to use a class: create a

More information

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects CmSc 150 Fundamentals of Computing I Lesson 28: Introduction to Classes and Objects in Java 1. Classes and Objects True object-oriented programming is based on defining classes that represent objects with

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9 Chapter 6 Arrays Chapter 7 Objects and Classes Chapter 8 Strings Chapter 9 Inheritance and Polymorphism GUI can be covered after 10.2, Abstract Classes Chapter 12 GUI Basics 10.2, Abstract Classes Chapter

More information

Practice Questions for Chapter 9

Practice Questions for Chapter 9 Practice Questions for Chapter 9 MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) An object is an instance of a. 1) A) program B) method C) class

More information

Oracle 1Z Java SE 8 Programmer I. Download Full Version :

Oracle 1Z Java SE 8 Programmer I. Download Full Version : Oracle 1Z0-808 Java SE 8 Programmer I Download Full Version : https://killexams.com/pass4sure/exam-detail/1z0-808 QUESTION: 121 And the commands: Javac Jump.java Java Jump crazy elephant is always What

More information

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java

More information

CS151 Principles of Computer Science I Fall 2018 Homework 6 S

CS151 Principles of Computer Science I Fall 2018 Homework 6 S 1. Exercise 19 : double Exercise 20 : String CS151 Principles of Computer Science I Fall 2018 Homework 6 S Exercise 21 : foo1 is a class method since it is declared static Exercise 22 : foo2 is an instance

More information

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes 1 CS257 Computer Science II Kevin Sahr, PhD Lecture 5: Writing Object Classes Object Class 2 objects are the basic building blocks of programs in Object Oriented Programming (OOP) languages objects consist

More information

Simple Java Reference

Simple Java Reference Simple Java Reference This document provides a reference to all the Java syntax used in the Computational Methods course. 1 Compiling and running... 2 2 The main() method... 3 3 Primitive variable types...

More information

Lecture 7: Classes and Objects CS2301

Lecture 7: Classes and Objects CS2301 Lecture 7: Classes and Objects NADA ALZAHRANI CS2301 1 What is OOP? Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly

More information

Comments are almost like C++

Comments are almost like C++ UMBC CMSC 331 Java Comments are almost like C++ The javadoc program generates HTML API documentation from the javadoc style comments in your code. /* This kind of comment can span multiple lines */ //

More information

Questions Answer Key Questions Answer Key Questions Answer Key

Questions Answer Key Questions Answer Key Questions Answer Key Benha University Term: 2 nd (2013/2014) Class: 2 nd Year Students Subject: Object Oriented Programming Faculty of Computers & Informatics Date: 26/4/2014 Time: 1 hours Exam: Mid-Term (C) Name:. Status:

More information

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes CS111: PROGRAMMING LANGUAGE II Lecture 1: Introduction to classes Lecture Contents 2 What is a class? Encapsulation Class basics: Data Methods Objects Defining and using a class In Java 3 Java is an object-oriented

More information

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects CS256 Computer Science I Kevin Sahr, PhD Lecture 11: Objects 1 Programs as Models remember: we write programs to solve realworld problems programs act as models of the real-world problem to be solved one

More information

Introduction to Classes and Objects How to manage data and actions together. Slides #10: Chapter

Introduction to Classes and Objects How to manage data and actions together. Slides #10: Chapter Topics 1) What is an object? What is a class? 2) How can we use objects? 3) How do we implement the functions of a class? Introduction to Classes and s How to manage data and actions together. Slides #10:

More information

CS1150 Principles of Computer Science Objects and Classes

CS1150 Principles of Computer Science Objects and Classes CS1150 Principles of Computer Science Objects and Classes Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Object-Oriented Thinking Chapters 1-8

More information

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini 24. Inheritance Java Fall 2009 Instructor: Dr. Masoud Yaghini Outline Superclasses and Subclasses Using the super Keyword Overriding Methods The Object Class References Superclasses and Subclasses Inheritance

More information

The Essence of OOP using Java, Nested Top-Level Classes. Preface

The Essence of OOP using Java, Nested Top-Level Classes. Preface The Essence of OOP using Java, Nested Top-Level Classes Baldwin explains nested top-level classes, and illustrates a very useful polymorphic structure where nested classes extend the enclosing class and

More information

CS101 Quiz No. 4 Nov. 30 & Dec. 1, 2015 Student Name: Kadir Can Çelik (Extended on Dec. 6, 2015)

CS101 Quiz No. 4 Nov. 30 & Dec. 1, 2015 Student Name: Kadir Can Çelik (Extended on Dec. 6, 2015) CS101 Quiz No. 4 Nov. 30 & Dec. 1, 2015 Student Name: Kadir Can Çelik (Extended on Dec. 6, 2015) CS101 Quiz No. 4 Nov. 30 & Dec. 1, 2015 Student Name: Kadir Can Çelik 1. Create a class called Circle with

More information

CS1004: Intro to CS in Java, Spring 2005

CS1004: Intro to CS in Java, Spring 2005 CS1004: Intro to CS in Java, Spring 2005 Lecture #13: Java OO cont d. Janak J Parekh janak@cs.columbia.edu Administrivia Homework due next week Problem #2 revisited Constructors, revisited Remember: a

More information

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver { Chapter 21- Using Generics Case Study: Geometric Bunch In this example a class called GeometricBunch is made to wrap around a list of GeometricObjects. Circle and Rectangle are subclasses of GeometricObject.

More information

Practice for Chapter 11

Practice for Chapter 11 Practice for Chapter 11 MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) Object-oriented programming allows you to derive new classes from existing

More information

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism rights reserved. 2 Motivation Suppose you want to define classes to model circles, rectangles, and

More information

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini 22. Inheritance Java Summer 2008 Instructor: Dr. Masoud Yaghini Outline Superclasses and Subclasses Using the super Keyword Overriding Methods The Object Class References Inheritance Object-oriented programming

More information

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding Java Class Design Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding eugeny.berkunsky@gmail.com http://www.berkut.mk.ua Objectives Implement encapsulation Implement inheritance

More information

Exception-Handling Overview

Exception-Handling Overview م.عبد الغني أبوجبل Exception Handling No matter how good a programmer you are, you cannot control everything. Things can go wrong. Very wrong. When you write a risky method, you need code to handle the

More information

CLASSES AND OBJECTS. Summer 2018

CLASSES AND OBJECTS. Summer 2018 CLASSES AND OBJECTS Summer 2018 OBJECT BASICS Everything in Java is part of a class This includes all methods (functions) Even the main() function is part of a class Classes are declared similar to C++

More information

Chapter 1-9, 12-13, 18, 20, 23 Review Slides. What is a Computer?

Chapter 1-9, 12-13, 18, 20, 23 Review Slides. What is a Computer? Chapter 1-9, 12-13, 18, 20, 23 Review Slides CS1: Java Programming Colorado State University Original slides by Daniel Liang Modified slides by Chris Wilcox rights reserved. 1 What is a Computer? A computer

More information

Use the scantron sheet to enter the answer to questions (pages 1-6)

Use the scantron sheet to enter the answer to questions (pages 1-6) Use the scantron sheet to enter the answer to questions 1-100 (pages 1-6) Part I. Mark A for True, B for false. (1 point each) 1. Abstraction allow us to specify an object regardless of how the object

More information

CSEN401 Computer Programming Lab. Topics: Introduction and Motivation Recap: Objects and Classes

CSEN401 Computer Programming Lab. Topics: Introduction and Motivation Recap: Objects and Classes CSEN401 Computer Programming Lab Topics: Introduction and Motivation Recap: Objects and Classes Prof. Dr. Slim Abdennadher 16.2.2014 c S. Abdennadher 1 Course Structure Lectures Presentation of topics

More information

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT). UNITII Classes Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT). It s a User Defined Data-type. The Data declared in a Class are called Data- Members

More information

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading COMP 202 CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading More on OO COMP 202 - Week 7 1 Static member variables So far: Member variables

More information

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College CISC 3115 TY3 C09a: Inheritance Hui Chen Department of Computer & Information Science CUNY Brooklyn College 9/20/2018 CUNY Brooklyn College 1 Outline Inheritance Superclass/supertype, subclass/subtype

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg

More information