Object Oriented Programming

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

Object Oriented Programming (II)

CS1150 Principles of Computer Science Objects and Classes

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

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

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

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

Inheritance and Polymorphism

Inheritance and Polymorphism

Abstract class & Interface

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

Chapter 8 Objects and Classes

CS Week 13. Jim Williams, PhD

ECOM 2324 COMPUTER PROGRAMMING II

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

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

Chapter 8 Objects and Classes Part 1

Arrays. Eng. Mohammed Abdualal

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

Final Exam 90 minutes Eng. Mohammed S. F. Abdual Al

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

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

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

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

Chapter 14 Abstract Classes and Interfaces

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

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

Lecture Notes Chapter #9_b Inheritance & Polymorphism

OO Programming Concepts

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

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

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

Lecture 7: Classes and Objects CS2301

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

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

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

Questions Answer Key Questions Answer Key Questions Answer Key

CS2 Assignment A1S The Simple Shapes Package

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

Industrial Programming

What is an Object. Industrial Programming. What is a Class (cont'd) What is a Class. Lecture 4: C# Objects & Classes

Lecture 6 Introduction to Objects and Classes

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

Chapter 11 Inheritance and Polymorphism

CMSC 132: Object-Oriented Programming II. Inheritance

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

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

Lecture Notes Chapter #9_c Abstract Classes & Interfaces

Inheritance (continued) Inheritance

Programming 2. Inheritance & Polymorphism

Computer Programming, I. Laboratory Manual. Final Exam Solution

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

ITI Introduction to Computing II

Eng. Mohammed S. Abdualal

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Object-Oriented Programming Concepts

Object-Oriented Programming Concepts

Inheritance. Unit 8. Summary. 8.1 Inheritance. 8.2 Inheritance: example. Inheritance Overriding of methods and polymorphism The class Object

ITI Introduction to Computing II

Eng. Mohammed Alokshiya

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

Introduction to Programming Using Python Lecture 4. Dr. Zhang COSC 1437 Fall, 2018 October 11, 2018

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

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

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Lesson11-Inheritance-Abstract-Classes. The GeometricObject case

Reusing Classes. Hendrik Speleers

Classes and Inheritance Extending Classes, Chapter 5.2

Chapter 11 Inheritance and Polymorphism

Inheritance (Deitel chapter 9)

25. Generic Programming

Lab 9: Creating a Reusable Class

SSE3052: Embedded Systems Practice

CSCE145 Test 2-Review 03/29/2015 Hongkai Yu

UFCE3T-15-M Object-oriented Design and Programming

Questions Answer Key Questions Answer Key Questions Answer Key

Reading Input from Text File

Questions Answer Key Questions Answer Key Questions Answer Key

Method Overriding in Java

Chapter 6 Class and Method

Methods. Eng. Mohammed Abdualal

Inheritance Motivation

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

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

Chapter 9. Objects and Classes

CS 149. Professor: Alvin Chao

CH. 2 OBJECT-ORIENTED PROGRAMMING

EECS 1001 and EECS 1030M, lab 01 conflict

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

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

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

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

F I N A L E X A M I N A T I O N

Lecture 7 Objects and Classes

In this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram.

CS 1301 Ch 8, Handout 2

We are on the GUI fast track path

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

Java Object Oriented Design. CSC207 Fall 2014

Inheritance (an intuitive description)

Transcription:

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 programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly identified. For example, a student, a desk, a circle, a button, and even a loan can all be viewed as objects. An object has a unique identity, state, and behavior. The state of an object (also known as its properties or attributes) is represented by data fields with their current values. A circle object, for example, has a data field radius, which is the property that characterizes a circle. A rectangle object has the data fields width and height, which are the properties that characterize a rectangle. The behavior of an object (also known as its actions) is defined by methods. To invoke a method on an object is to ask the object to perform an action. For example, you may define methods named getarea() and getperimeter() for circle objects. A circle object may invoke getarea() to return its area and getperimeter() to return its perimeter. You may also define the setradius(radius) method. A circle object can invoke this method to change its radius. Objects of the same type are defined using a common class. A class is a template, blueprint, or contract that defines what an object s data fields and methods will be. An object is an instance of a class. You can create many instances of a class. Creating an instance is referred to as instantiation. The terms object and instance are often interchangeable. The relationship between classes and objects is analogous to that between an apple-pie recipe and apple pies: You can make as many apple pies as you want from a single recipe. A Java class uses variables to define data fields and methods to define actions. Additionally, a class provides methods of a special type, known as constructors, which are invoked to create a new object. A constructor can perform any action, but constructors are designed to perform initializing actions, such as initializing the data fields of objects. 2

Class Name: Person Data Fields: fname is lname is age is ID is Methods: getfullname A class Template Person Object 1 Data Fields: fname is Emad lname is Mostafa age is 21 ID is 120130000 Person Object 2 Data Fields: fname is Hamza lname is Emad age is 21 ID is 120130001 Two objects of the Person class A class is a construct that defines objects of the same type. Here is a java code to define the above class Person : class Person { // Fields String fname; String lname; int age; int ID; class Person // A Constructor public Person() { // Another Constructor public Person(String fname, String lname, int age, int ID) { this.fname = fname; this.lname = lname; this.age = age; this.id = ID; // A method public String getfullname() { String fullname = fname + " " + lname; return fullname; 3

Then you can create an instance p1 of the class Person, using the new operator, and assign values to its fields using. operator: class Main public class Main { public static void main(string[] args) { // Create object p1 using the first constructor (new Person()) Person p1 = new Person(); p1.fname = "Emad"; p1.lname = "Mostafa"; p1.age = 21; p1.id = 120130000; System.out.println(p1.getFullName()); // Create object p1 using the // second constructor (new Person(fName, lname, age, id)) Person p2 = new Person("Hamza", "Emad", 21, 120130001); System.out.println(p2.getFullName()); 4

Constructors Constructors are a special kind of methods that invoked to create an object using the new operator. They have three peculiarities: A constructor 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. The constructor has exactly the same name as its defining class. Like regular methods, constructors can be overloaded (i.e., multiple constructors can have the same name but different signatures), making it easy to construct objects with different initial data values. Constructors are used to construct objects. To construct an object from a class, invoke a constructor of the class using the new operator, as follows: new ClassName(arguments); Note: A class may be defined without constructors. In this case, a public noarg constructor with an empty body is implicitly defined in the class. This constructor, called a default constructor, is provided automatically only if no constructors are explicitly defined in the class. The this Reference The this keyword is the name of a reference that an object can use to refer to itself. You can use the this keyword to reference the object s instance members. It can also be used inside a constructor to invoke another constructor of the same class. 5

Example: Design a class named Car to represent a car. The class contains: Two String data fields named model and color that specify the model and color of the car. Three int data fields named capacity, maxspeed and manufacturingyear that specify the capacity, maximum speed and manufacturing year of the car. The default value of capacity is 4. A no-arg constructor that creates a default car. A constructor that creates a car with the specified capacity, maxspeed, manufacturingyear, model and color. A method named tostring() that returns all information about the car as string. Then create two instances of the Car class, with the following info: 1. Model >> I30, color >> Silver, capacity >> 4, maxspeed >> 240, manufacturingyear >> 2010. 2. Model >> Mercedes 250, color >> Red, capacity >> 4, maxspeed >> 260, manufacturingyear >> 2000. Use tostring() method to print the information of a car. public class Main { Code public static void main(string[] args) { Car c1 = new Car("I30", "Silver", 4, 240, 2010); Car c2 = new Car("Mercedes 250", "Red", 4, 260, 2000); System.out.println(c1.toString()); System.out.println(); System.out.println(c2.toString()); class Car { String model; String color; int capacity; int maxspeed; int manufacturingyear; 6

public Car() { public Car(String model, String color, int capacity, int maxspeed, int manufacturingyear) { this.model = model; this.color = color; this.capacity = capacity; this.maxspeed = maxspeed; this.manufacturingyear = manufacturingyear; @Override public String tostring() { String s = String.format("%-20s%s\n%-20s%s\n%-20s%s\n%-20s%s\n%-20s%s", "Model:", model, "Color:", color, "Capacity:", capacity, "Maximum Speed:", maxspeed, "Manufacturing Year:", manufacturingyear); return s; Model: I30 Color: Silver Capacity: 4 Maximum Speed: 240 Manufacturing Year: 2010 Model: Mercedes 250 Color: Red Capacity: 4 Maximum Speed: 260 Manufacturing Year: 2000 Output 7