Lecture 6 Introduction to Objects and Classes

Size: px
Start display at page:

Download "Lecture 6 Introduction to Objects and Classes"

Transcription

1 Lecture 6 Introduction to Objects and Classes

2 Outline Basic concepts Recap Computer programs Programming languages Programming paradigms Object oriented paradigm-objects and classes in Java Constructors Memory model and Garbage collection Static Keyword Visibility Modifiers

3 Basic concepts 3

4 Some types of computer programs Systems programs: programs that are needed to keep all the hardware and software systems running together smoothly, e.g., Operating Systems Application programs: programs that people use to get their work done, e.g., Word Processor, Game programs etc. Compilers: the computer understands only one language: machine language. Machine language is in the form of ones and zeros. Since it is highly impractical for people to create programs out of zeros and ones, there must be a way of translating or converting a language which we understand into machine language, for this purpose, there exists compilers 4

5 What is programming Telling the computer what I want it to do A program is a recipe for action: it tells the computer how to act in response to each possible input A computer takes input a recipe (sequence of steps) and will act like what is described in that recipe There are different types of programming languages that can be used to create programs, but regardless of what language you use, these instructions are translated into machine language that can be understood by computers 5

6 Categories of programming languages High-level Programming Languages A high-level programming language is a programming language that is more user friendly, i.e., easy to read and write Examples are Basic, Fortran, C, C++, Java, Haskell etc. Low-level Assembly Language Assembly languages are similar to machine languages, but they are much easier to program in because they allow a programmer to substitute names for numbers 6

7 Programming paradigms A programming paradigm is a fundamental style or way of computer programming Some well known programming paradigms Imperative Control flow is an explicit sequence of commands. Examples include FORTRAN, Algol, COBOL, Pascal, C Declarative Programs state the result you want, not how to get it. Examples include PROLOG Procedural Imperative programming with procedure calls. Examples include, Ada, ALGOL, BCPL, C, C++, C# Functional (Applicative) Computation proceeds by (nested) function calls that avoid any global state. Examples include, Haskell, Erlang, ML, Scheme Object-Oriented Computation is effected by sending messages to objects; objects have state and behavior. Examples include, Delphi/Object Pascal, C++, Java, C# 7

8 The Object Oriented Paradigm One of the primary features of the O-O paradigm is its ability to manage complexity. Complex complicated Complex = composed of many simple parts related to one another Complicated = not well understood, or explained Clear identification of system entities (objects) Abstract classification of system entities (classes) 8

9 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. 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 behaviors. The state of an object consists of a set of data fields (also known as properties) with their current values. The behavior of an object is defined by a set of methods. 9

10 Objects If we consider the real-world we can find many objects around us, Cars, Dogs, School, Office, etc. All these objects have a state and behaviour For example, me/you could be the object your state can be represented using your name, address, height, weight, color of eyes, date of birth, and so on your behavior crying, laughing, singing etc. If we compare the software object with a real world object, we can find very similar characteristics in them A software object's state is stored in fields and behaviour is shown via methods 10

11 Object Oriented Paradigms: foundational characteristics Objects and attributes Abstraction Encapsulation Inheritance Composition Modularity Polymorphism Message communication 11

12 Objects: example Objects are identified from the real world The data associated with each object are identified and modelled. The various behaviours of each object are identified and modelled. Interactions between objects are modelled. 12

13 Objects: example Objects are identified from the real world The data associated with each object are identified and modelled. The various behaviours of each object are identified and modelled. Interactions between objects are modelled. Attributes: defining states Bella Name Age Max 1 year 1.5 years 13

14 Objects: example Objects are identified from the real world The data associated with each object are identified and modelled. The various behaviours of each object are identified and modelled. Interactions between objects are modelled. Attributes: defining states Bella Name Age Max 1 year 1.5 years can meow() can eat() can play() can read() Methods: defining behaviour 14

15 Classes Classes are constructs that define objects of the same type Cat name: String age: double meow(): void eat(): void play(): void read(): void Class name Attributes/Data fields Methods 15

16 Classes and objects Cat Class name name: String age: double meow(): void eat(): void play(): void Class: is the Blueprint Attributes/Data fields Methods cat1: Cat name = Bela ; age = 1; cat2: Cat name = Max ; age = 1.5; Object: Manifestation of a class 16

17 Java classes (Cat example) The basic syntax for a class definition: class ClassName [extends SuperClassName] { [fields declaration] [methods declaration] Example: public class Cat { //cat class will be discussed later 17

18 Java classes Adding Fields/Attributes: Class Circle with fields public class Cat { String name; // name of the Cat The fields (data) are also called the instance variables. 18

19 Java classes Adding Methods: Class Cat with a Method public class Cat { String name; // name of the Cat public void meow(){ System.out.println("Cat Meowing"); The fields (data) are also called the instance variables. 19

20 What a class does? Allows defining abstract data types By declaring the Circle class, we have created a new data type Data Abstraction Then we can define variables (objects) of that type. Cat mycat = new Cat(); Cat yourcat = new Cat(); 20

21 Creating objects of a class Objects are created dynamically using the new keyword. mycat and yourcat refer to Cat objects Cat mycat = new Cat(); Cat yourcat = new Cat(); 21

22 What can we do when we create an object of a class? We can access/manipulate Object/Circle Data, execute behaviour Similar to C/C++ syntax for accessing data defined in a structure. ObjectName.VariableName ObjectName.MethodName(parameter-list) 22

23 Accessing Data public class Cat { String name; // name of the Cat public void meow(){ System.out.println("Cat Meowing"); Cat mycat = new Cat(); mycat.name = "Bela"; Cat yourcat = new Cat(); yourcat.name = "Max"; 23

24 Executing method public class Cat { double age; // age of the Cat String name; // name of the Cat public void meow(){ System.out.println("Cat Meowing"); Cat mycat = new Cat(); Cat yourcat = new Cat(); mycat.meow(); yourcat.meow(); 24

25 The executable code //Cat.java public class Cat { String name; // name of the Cat public void meow(){ System.out.println( Cat Meowing ); class mycattest{ public static void main(string args[]) { Cat mycat = new Cat(); Cat yourcat = new Cat(); mycat.meow(); yourcat.meow(); Output: Cat Meowing Cat Meowing 25

26 The executable code //Cat.java public class Cat { String name; // name of the Cat public void meow(){ System.out.println(name+ " Meowing"); class mycattest{ public static void main(string args[]) { Cat mycat = new Cat(); mycat.name = "Bela"; Cat yourcat = new Cat(); yourcat.name = "Max"; mycat.meow(); yourcat.meow(); Output: Bela Meowing Max Meowing 26

27 Constructors 27

28 What happens if the following code has been executed? //Cat.java public class Cat { String name; // name of the Cat public void meow(){ System.out.println(name+ " Meowing"); class mycattest{ public static void main(string args[]){ Cat mycat = new Cat(); //mycat.name = "Bela"; Cat yourcat = new Cat(); //yourcat.name = "Max"; mycat.meow(); yourcat.meow(); 28

29 What happens if the following code has been executed? //Cat.java public class Cat { String name; // name of the Cat public void meow(){ System.out.println(name+ " Meowing"); class mycattest{ public static void main(string args[]){ Output: null Meowing null Meowing Cat mycat = new Cat(); //mycat.name = "Bela"; Cat yourcat = new Cat(); //yourcat.name = "Max"; mycat.meow(); yourcat.meow(); 29

30 What happens if the following code has been executed? //Cat.java public class Cat { String name; // name of the Cat Cat() { public void meow(){ System.out.println(name+ " Meowing"); class mycattest{ public static void main(string args[]){ Cat mycat = new Cat(); //mycat.name = "Bela"; Cat yourcat = new Cat(); //yourcat.name = "Max"; mycat.meow(); yourcat.meow(); 30

31 What happens if the following code has been executed? //Cat.java public class Cat { String name; // name of the Cat Cat() { public void meow(){ System.out.println(name+ " Meowing"); class mycattest{ public static void main(string args[]){ Output: null Meowing null Meowing Cat mycat = new Cat(); //mycat.name = "Bela"; Cat yourcat = new Cat(); //yourcat.name = "Max"; mycat.meow(); yourcat.meow(); 31

32 Constructor: Basic characteristics public class Cat { String name; // name of the Cat Cat(){ //constructor public void meow(){ System.out.println(name+ " Meowing"); Constructor: Basic characteristics Constructors have the same name as that of the class they belong to. Constructors have no return type (not even void). Constructors are executed when objects of that class are created. Constructors initialize objects and allocate appropriate memory to objects. 32

33 Default constructor public class Cat { String name; // name of the Cat Cat(){ //default constructor public void meow(){ System.out.println(name+ " Meowing"); The above constructor is called Default constructor 33

34 Default constructor contd. If you don t implement any constructor in your class, the Java compiler inserts default constructor into your code on your behalf. public class Cat { String name; //name of the Cat public void meow(){ System.out.println(name+ " Meowing"); compiler public class Cat { String name; //name of the Cat Cat() { public void meow(){ System.out.println(name+ " Meowing"); 34

35 No-argument constructor //Cat.java public class Cat { String name; // name of the Cat Cat(){ name = "Unknown cat"; public void meow(){ System.out.println(name+ " Meowing"); Output:?? class mycattest{ public static void main(string args[]){ Cat mycat = new Cat(); //mycat.name = "Bela"; Cat yourcat = new Cat(); //yourcat.name = "Max"; mycat.meow(); yourcat.meow(); 35

36 No-argument constructor //Cat.java public class Cat { String name; // name of the Cat Cat(){ name = "Unknown cat"; public void meow(){ System.out.println(name+ " Meowing"); class mycattest{ public static void main(string args[]){ Cat mycat = new Cat(); //mycat.name = "Bela"; Cat yourcat = new Cat(); //yourcat.name = "Max"; mycat.meow(); yourcat.meow(); Output: Unknown cat Meowing Unknown cat Meowing 36

37 No-argument constructor public class Cat { String name; // name of the Cat Cat(){ name = "Unknown cat"; public void meow(){ System.out.println(name+ " Meowing"); Are no-argument constructor and default constructor (empty body) same? Exercise! 37

38 Parameterised constructor //Cat.java public class Cat { String name; // name of the Cat Cat(String catname){ name = catname; public void meow(){ System.out.println(name+ " Meowing"); Cat mycat = new Cat("Bela"); // creating object 38

39 Parameterised constructor contd. //Cat.java public class Cat { String name; // name of the Cat Cat(String catname){ name = catname; public void meow(){ System.out.println(name+ " Meowing"); class mycattest{ public static void main(string args[]){ Cat mycat = new Cat("Bela"); Cat yourcat = new Cat ("Max"); mycat.meow(); yourcat.meow(); Output: Bela Meowing Max Meowing 39

40 Parameterised constructor contd. //Cat.java public class Cat { String name; // name of the Cat Cat(String catname){ name = catname; public void meow(){ System.out.println(name+ " Meowing"); class mycattest{ public static void main(string args[]){ Cat hiscat = new Cat(); Cat mycat = new Cat("Bela"); Cat yourcat = new Cat ("Max"); mycat.meow(); yourcat.meow(); Output:?? 40

41 Parameterised constructor contd. //Cat.java public class Cat { String name; // name of the Cat Cat(String catname){ name = catname; public void meow(){ System.out.println(name+ " Meowing"); class mycattest{ public static void main(string args[]){ Cat hiscat = new Cat(); Cat mycat = new Cat("Bela"); Cat yourcat = new Cat ("Max"); mycat.meow(); yourcat.meow(); Output:?? Constructor Cat in class Cat cannot be applied to given type 41

42 When compiler supplies a default constructor? Important: A default constructor is provided automatically only if no constructors are explicitly defined in the class. 42

43 Parameterised constructor contd. //Cat.java public class Cat { String name; // name of the Cat Cat() { Cat(String catname){ name = catname; public void meow(){ System.out.println(name+ " Meowing"); class mycattest{ public static void main(string args[]){ Cat hiscat = new Cat(); Cat mycat = new Cat("Bela"); Cat yourcat = new Cat ("Max"); mycat.meow(); yourcat.meow(); Output: Bela Meowing Max Meowing 43

44 Parameterised constructor contd. //Cat.java public class Cat { String name; // name of the Cat Cat() { Cat(String catname){ name = catname; public void meow(){ System.out.println(name+ " Meowing"); class mycattest{ public static void main(string args[]){ Cat hiscat = new Cat(); Cat mycat = new Cat("Bela"); Cat yourcat = new Cat ("Max"); mycat.meow(); yourcat.meow(); hiscat.meow(); Output: Bela Meowing Max Meowing null Meowing 44

45 Image source: google 45

46 Memory model and Garbage collection 46

47 Memory allocation frame Compiled code, static variables, constant, and other class variables Code and static data Heap (objects) LOW Dynamic memeory Functions/Methods Stack (local variables) HIGH 47

48 Object references Objects mycat.name = Bela Class:Cat String name yourcat.name = Max 48

49 Object references contd. An object in Java is identified by its address in memory. That address is called a reference. As an example, when Java evaluates the declaration Cat mycat = new Cat(); it allocates heap space for the new Cat object. The local variable mycat is allocated in the current stack frame and is assigned the value (address), which identifies the object. 49

50 Heap mycat.name 1000 yourcat.name 1008 Cat mycat = new Cat("Bela"); Cat yourcat = new Cat ("Max"); 1016 yourcat 1000 mycat Stack 50

51 Heap mycat.name 1000 Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. yourcat.name Cat mycat = new Cat("Bela"); Cat yourcat = new Cat ("Max"); yourcat mycat Stack 51

52 Garbage Collection: An example mycat = yourcat; After the above assignment statement, mycat points to the same object referenced by yourcat. The object previously referenced by mycat is no longer referenced. This object is known as garbage. Garbage is automatically collected by JVM. 52

53 Static Keyword 53

54 What is static The static keyword is used when a member variable of a class has to be shared between all the instances (objects) of the class. All static variables and methods belong to the class and not to any instance of the class. They are called class variables. main() is static Even though it s in a class definition, no instance of the class exists when main starts executing 54

55 When can we access static variables When a class is loaded by the JVM all the static variables and methods are available for use. Hence we don t need to create any instance (object) of the class for using the static variables or methods. Variables which don t have static keyword in the definition are implicitly non static. Must be initialized in their declarations, or else the compiler will initialize it with a default value. 55

56 Static variable example //Cat.java public class Cat { String name; // name of the Cat public static int count = 0; //number of objects in memory Cat(String catname){ name = catname; count++; public static int getcount(){ return count; public void meow(){ System.out.println(name+ " Meowing"); Output:?? class mycattest{ public static void main(string args[]){ Cat mycat = new Cat("Bela"); Cat yourcat = new Cat ( Max"); System.out.println("Count= "+Cat.getCount()); 56

57 Static variable example //Cat.java public class Cat { String name; // name of the Cat public static int count = 0; //number of objects in memory Cat(String catname){ name = catname; count++; public static int getcount(){ return count; public void meow(){ System.out.println(name+ " Meowing"); Output: Count= 2 class mycattest{ public static void main(string args[]){ Cat mycat = new Cat("Bela"); Cat yourcat = new Cat ( Max"); System.out.println("Count= "+Cat.getCount()); 57

58 Visibility Modifiers 58

59 Visibility Modifiers By default, the class, variable, or method can be accessed by any class in the same package. 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. 59

60 The private modifier restricts access to within a class, the default modifier restricts access to within a package, and the public modifier enables unrestricted access. 60

61 The default modifier on a class restricts access to within a package, and the public modifier enables unrestricted access. 61

62 NOTE An object cannot access its private members, as shown in (b). It is OK, however, if the object is declared in its own class, as shown in (a). 62

63 Why Data Fields Should Be private? To protect data. To make code easy to maintain. Data Fields should be declared private unless there is a good reason for not doing so 63

64 Reference Chapter 9, Introduction to Java Programming, Liang. 64

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

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

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

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

CSC 533: Organization of Programming Languages. Spring 2005

CSC 533: Organization of Programming Languages. Spring 2005 CSC 533: Organization of Programming Languages Spring 2005 Language features and issues variables & bindings data types primitive complex/structured expressions & assignments control structures subprograms

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

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

Exercise: Singleton 1

Exercise: Singleton 1 Exercise: Singleton 1 In some situations, you may create the only instance of the class. 1 class mysingleton { 2 3 // Will be ready as soon as the class is loaded. 4 private static mysingleton Instance

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

C10: Garbage Collection and Constructors

C10: Garbage Collection and Constructors CISC 3120 C10: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/5/2018 CUNY Brooklyn College 1 Outline Recap OOP in Java: composition &

More information

Chapter 2: Java OOP I

Chapter 2: Java OOP I Chapter 2: Java OOP I Yang Wang wyang AT njnet.edu.cn Outline OO Concepts Class and Objects Package Field Method Construct and Initialization Access Control OO Concepts Object Oriented Methods Object An

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

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors Agenda

More information

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright Java Basics Object Orientated Programming in Java Benjamin Kenwright Outline Essential Java Concepts Syntax, Grammar, Formatting, Introduce Object-Orientated Concepts Encapsulation, Abstract Data, OO Languages,

More information

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS121/IS223. Object Reference Variables. Dr Olly Gotel CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors CS121/IS223

More information

Inheritance and Interfaces

Inheritance and Interfaces Inheritance and Interfaces Object Orientated Programming in Java Benjamin Kenwright Outline Review What is Inheritance? Why we need Inheritance? Syntax, Formatting,.. What is an Interface? Today s Practical

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

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

Chapter 5. Names, Bindings, and Scopes

Chapter 5. Names, Bindings, and Scopes Chapter 5 Names, Bindings, and Scopes Chapter 5 Topics Introduction Names Variables The Concept of Binding Scope Scope and Lifetime Referencing Environments Named Constants 1-2 Introduction Imperative

More information

Instance Members and Static Members

Instance Members and Static Members Instance Members and Static Members You may notice that all the members are declared w/o static. These members belong to some specific object. They are called instance members. This implies that these

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

Concepts Introduced in Chapter 7

Concepts Introduced in Chapter 7 Concepts Introduced in Chapter 7 Storage Allocation Strategies Static Stack Heap Activation Records Access to Nonlocal Names Access links followed by Fig. 7.1 EECS 665 Compiler Construction 1 Activation

More information

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction Lecture 13: Object orientation Object oriented programming Introduction, types of OO languages Key concepts: Encapsulation, Inheritance, Dynamic binding & polymorphism Other design issues Smalltalk OO

More information

Example: Count of Points

Example: Count of Points Example: Count of Points 1 public class Point { 2... 3 private static int numofpoints = 0; 4 5 public Point() { 6 numofpoints++; 7 } 8 9 public Point(int x, int y) { 10 this(); // calling Line 5 11 this.x

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

CPS 506 Comparative Programming Languages. Programming Language

CPS 506 Comparative Programming Languages. Programming Language CPS 506 Comparative Programming Languages Object-Oriented Oriented Programming Language Paradigm Introduction Topics Object-Oriented Programming Design Issues for Object-Oriented Oriented Languages Support

More information

NOTE: Answer ANY FOUR of the following 6 sections:

NOTE: Answer ANY FOUR of the following 6 sections: A-PDF MERGER DEMO Philadelphia University Lecturer: Dr. Nadia Y. Yousif Coordinator: Dr. Nadia Y. Yousif Internal Examiner: Dr. Raad Fadhel Examination Paper... Programming Languages Paradigms (750321)

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

Create a Java project named week10

Create a Java project named week10 Objectives of today s lab: Through this lab, students will examine how casting works in Java and learn about Abstract Class and in Java with examples. Create a Java project named week10 Create a package

More information

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline CS 0 Lecture 8 Chapter 5 Louden Outline The symbol table Static scoping vs dynamic scoping Symbol table Dictionary associates names to attributes In general: hash tables, tree and lists (assignment ) can

More information

C++ & Object Oriented Programming Concepts The procedural programming is the standard approach used in many traditional computer languages such as BASIC, C, FORTRAN and PASCAL. The procedural programming

More information

10. Abstract Data Types

10. Abstract Data Types 10. Abstract Data Types 11.1 The Concept of Abstraction The concept of abstraction is fundamental in programming Nearly all programming languages support process abstraction with subprograms Nearly all

More information

Lecture 2: Java & Javadoc

Lecture 2: Java & Javadoc Lecture 2: Java & Javadoc CS 62 Fall 2018 Alexandra Papoutsaki & William Devanny 1 Instance Variables or member variables or fields Declared in a class, but outside of any method, constructor or block

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

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003 Outline Object Oriented Programming Autumn 2003 2 Course goals Software design vs hacking Abstractions vs language (syntax) Java used to illustrate concepts NOT a course about Java Prerequisites knowledge

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

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner. HAS-A Relationship Association is a relationship where all objects have their own lifecycle and there is no owner. For example, teacher student Aggregation is a specialized form of association where all

More information

Example: Fibonacci Numbers

Example: Fibonacci Numbers Example: Fibonacci Numbers Write a program which determines F n, the (n + 1)-th Fibonacci number. The first 10 Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34. The sequence of Fibonacci numbers

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

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++

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

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

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005 Lecture 8 Classes and Objects Part 2 MIT AITI June 15th, 2005 1 What is an object? A building (Strathmore university) A desk A laptop A car Data packets through the internet 2 What is an object? Objects

More information

C11: Garbage Collection and Constructors

C11: Garbage Collection and Constructors CISC 3120 C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017 CUNY Brooklyn College 1 Outline Recap Project progress and lessons

More information

CMSC 331 Final Exam Section 0201 December 18, 2000

CMSC 331 Final Exam Section 0201 December 18, 2000 CMSC 331 Final Exam Section 0201 December 18, 2000 Name: Student ID#: You will have two hours to complete this closed book exam. We reserve the right to assign partial credit, and to deduct points for

More information

Nested Loops. A loop can be nested inside another loop.

Nested Loops. A loop can be nested inside another loop. Nested Loops A loop can be nested inside another loop. Nested loops consist of an outer loop and one or more inner loops. Each time the outer loop is repeated, the inner loops are reentered, and started

More information

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism Programming using C# LECTURE 07 Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism What is Inheritance? A relationship between a more general class, called the base class

More information

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

More About Objects. Zheng-Liang Lu Java Programming 255 / 282 More About Objects Inheritance: passing down states and behaviors from the parents to their children. Interfaces: requiring objects for the demanding methods which are exposed to the outside world. Polymorphism

More information

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage Storage 1 Variables and Updating Outline Composite Variables Total and selective updating Array variables Storables Lifetime Local and global variables Heap variables Persistent variables Garbage collection

More information

D Programming Language

D Programming Language Group 14 Muazam Ali Anil Ozdemir D Programming Language Introduction and Why D? It doesn t come with a religion this is written somewhere along the overview of D programming language. If you actually take

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

class objects instances Fields Constructors Methods static

class objects instances Fields Constructors Methods static Class Structure Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance variables)that hold the data for each object Constructors that

More information

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Atelier Java - J1. Marwan Burelle.  EPITA Première Année Cycle Ingénieur. marwan.burelle@lse.epita.fr http://wiki-prog.kh405.net Plan 1 2 Plan 3 4 Plan 1 2 3 4 A Bit of History JAVA was created in 1991 by James Gosling of SUN. The first public implementation (v1.0) in 1995.

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Objectives To review the concepts and terminology of object-oriented programming To discuss some features of objectoriented design 1-2 Review: Objects In Java and other Object-Oriented

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

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A. HAS-A Relationship Association is a weak relationship where all objects have their own lifetime and there is no ownership. For example, teacher student; doctor patient. If A uses B, then it is an aggregation,

More information

Programming Languages 2nd edition Tucker and Noonan"

Programming Languages 2nd edition Tucker and Noonan Programming Languages 2nd edition Tucker and Noonan" " Chapter 1" Overview" " A good programming language is a conceptual universe for thinking about programming. " " " " " " " " " " " " "A. Perlis" "

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

Arrays Classes & Methods, Inheritance

Arrays Classes & Methods, Inheritance Course Name: Advanced Java Lecture 4 Topics to be covered Arrays Classes & Methods, Inheritance INTRODUCTION TO ARRAYS The following variable declarations each allocate enough storage to hold one value

More information

Lesson 6 Introduction to Object-Oriented Programming

Lesson 6 Introduction to Object-Oriented Programming Lesson 6 Introduction to Object-Oriented Programming Programming Grade in Computer Engineering Outline 1. Motivation 2. Classes, objects and attributes 3. Constructors 4. Methods 5. Composition 6. Object

More information

Example: Count of Points

Example: Count of Points Example: Count of Points 1 class Point { 2... 3 private static int numofpoints = 0; 4 5 Point() { 6 numofpoints++; 7 } 8 9 Point(int x, int y) { 10 this(); // calling the constructor with no input argument;

More information

Weeks 6&7: Procedures and Parameter Passing

Weeks 6&7: Procedures and Parameter Passing CS320 Principles of Programming Languages Weeks 6&7: Procedures and Parameter Passing Jingke Li Portland State University Fall 2017 PSU CS320 Fall 17 Weeks 6&7: Procedures and Parameter Passing 1 / 45

More information

Java: introduction to object-oriented features

Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer

More information

Lecture 7 Objects and Classes

Lecture 7 Objects and Classes Lecture 7 Objects and Classes An Introduction to Data Abstraction MIT AITI June 13th, 2005 1 What do we know so far? Primitives: int, double, boolean, String* Variables: Stores values of one type. Arrays:

More information

Implementing Subprograms

Implementing Subprograms Implementing Subprograms 1 Topics The General Semantics of Calls and Returns Implementing Simple Subprograms Implementing Subprograms with Stack-Dynamic Local Variables Nested Subprograms Blocks Implementing

More information

Data Structures using OOP C++ Lecture 3

Data Structures using OOP C++ Lecture 3 References: th 1. E Balagurusamy, Object Oriented Programming with C++, 4 edition, McGraw-Hill 2008. 2. Robert L. Kruse and Alexander J. Ryba, Data Structures and Program Design in C++, Prentice-Hall 2000.

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

UFCE3T-15-M Object-oriented Design and Programming

UFCE3T-15-M Object-oriented Design and Programming UFCE3T-15-M Object-oriented Design and Programming Block1: Objects and Classes Jin Sa 27-Sep-05 UFCE3T-15-M Programming part 1 Objectives To understand objects and classes and use classes to model objects.

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 02: Using Objects MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Using Objects 2 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management

More information

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8 Subroutines and Control Abstraction Textbook, Chapter 8 1 Subroutines and Control Abstraction Mechanisms for process abstraction Single entry (except FORTRAN, PL/I) Caller is suspended Control returns

More information

Create a Java project named week9

Create a Java project named week9 Objectives of today s lab: Through this lab, students will explore a hierarchical model for object-oriented design and examine the capabilities of the Java language provides for inheritance and polymorphism.

More information

Imperative Languages!

Imperative Languages! Imperative Languages! Java is an imperative object-oriented language. What is the difference in the organisation of a program in a procedural and an objectoriented language? 30 class BankAccount { private

More information

Concepts of Programming Languages

Concepts of Programming Languages Concepts of Programming Languages Lecture 10 - Object-Oriented Programming Patrick Donnelly Montana State University Spring 2014 Patrick Donnelly (Montana State University) Concepts of Programming Languages

More information

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT Programming Languages & Paradigms PROP HT 2011 Lecture 4 Subprograms, abstractions, encapsulation, ADT Beatrice Åkerblom beatrice@dsv.su.se Course Council Meeting on friday! Talk to them and tell them

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

1.00 Lecture 8. Using An Existing Class, cont.

1.00 Lecture 8. Using An Existing Class, cont. .00 Lecture 8 Classes, continued Reading for next time: Big Java: sections 7.9 Using An Existing Class, cont. From last time: is a Java class used by the BusTransfer class BusTransfer uses objects: First

More information

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst Department of Computer Science Why C? Low-level Direct access to memory WYSIWYG (more or less) Effectively

More information

Sri Vidya College of Engineering & Technology

Sri Vidya College of Engineering & Technology UNIT I INTRODUCTION TO OOP AND FUNDAMENTALS OF JAVA 1. Define OOP. Part A Object-Oriented Programming (OOP) is a methodology or paradigm to design a program using classes and objects. It simplifies the

More information

System Analysis and Design. Introduction: Object Oriented Design

System Analysis and Design. Introduction: Object Oriented Design System Analysis and Design Introduction: Object Oriented Design Salahaddin University College of Engineering Software Engineering Department 2011-2012 Amanj Sherwany http://www.amanj.me/wiki/doku.php?id=teaching:su:system_analysis_and_design

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year Object Oriented Programming Assistant Lecture Omar Al Khayat 2 nd Year Syllabus Overview of C++ Program Principles of object oriented programming including classes Introduction to Object-Oriented Paradigm:Structures

More information

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc. Chapter 13 Object Oriented Programming Contents 13.1 Prelude: Abstract Data Types 13.2 The Object Model 13.4 Java 13.1 Prelude: Abstract Data Types Imperative programming paradigm Algorithms + Data Structures

More information

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism Block 1: Introduction to Java Unit 4: Inheritance, Composition and Polymorphism Aims of the unit: Study and use the Java mechanisms that support reuse, in particular, inheritance and composition; Analyze

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario The Story So Far... Classes as collections of fields and methods. Methods can access fields, and

More information

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University G22.2110-001 Programming Languages Spring 2010 Lecture 6 Robert Grimm, New York University 1 Review Last week Function Languages Lambda Calculus SCHEME review 2 Outline Promises, promises, promises Types,

More information

LECTURE 2 (Gaya College of Engineering)

LECTURE 2 (Gaya College of Engineering) LECTURE 2 (Gaya College of Engineering) 1) CHARACTERISTICS OF OBJECTS: Object is an instance of a class. So, it is an active entity. Objects have three basic characteristics. They are- State: An object

More information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:

More information

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility Quiz Review Q1. What is the advantage of binding things as early as possible? Is there any advantage to delaying binding? Answer: Early binding generally leads to greater efficiency (compilation approach)

More information

Goal of lecture. Object-oriented Programming. Context of discussion. Message of lecture

Goal of lecture. Object-oriented Programming. Context of discussion. Message of lecture Goal of lecture Object-oriented Programming Understand inadequacies of class languages like Ur- Java Extend Ur-Java so it becomes an object-oriented language Implementation in SaM heap allocation of objects

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED 엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED Outline - Questionnaire Results - Java Overview - Java Examples

More information

Object Orientated Programming Details COMP360

Object Orientated Programming Details COMP360 Object Orientated Programming Details COMP360 The ancestor of every action is a thought. Ralph Waldo Emerson Three Pillars of OO Programming Inheritance Encapsulation Polymorphism Inheritance Inheritance

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

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes, Chapter 5 Basic Semantics Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes, and the Environment Variables

More information

Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction

Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction Class Interaction There are 3 types of class interaction. One

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

The University Of Michigan. EECS402 Lecture 06. Andrew M. Morgan. Savitch Ch. 6 Intro To OOP Classes Objects ADTs.

The University Of Michigan. EECS402 Lecture 06. Andrew M. Morgan. Savitch Ch. 6 Intro To OOP Classes Objects ADTs. The University Of Michigan Lecture 06 Andrew M. Morgan Savitch Ch. 6 Intro To OOP Classes Objects ADTs Complexity Increases A Little History Early computing Cheap Programmers, Expensive Computers Binary

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application Fifth Generation CS 4100 LISP From Principles of Programming Languages: Design, Evaluation, and Implementation (Third Edition, by Bruce J. MacLennan, Chapters 9, 10, 11, and based on slides by Istvan Jonyer

More information

Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming Introduction to Object-Oriented Programming Objects and classes Abstract Data Types (ADT). Encapsulation OOP: Introduction 1 Pure Object-Oriented Languages Five rules [Source: Alan Kay]: Everything in

More information