Object Oriented Programming (II)

Similar documents
Object Oriented Programming

Abstract class & Interface

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

Principles of Object Oriented Programming. Lecture 4

Arrays. Eng. Mohammed Abdualal

Eng. Mohammed S. Abdualal

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

Eng. Mohammed Alokshiya

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Fundamentals of Object Oriented Programming

This Week. Fields and Variables. W05 Example 1: Variables & Fields. More on Java classes. Constructors. Modifiers

* Mrs. K.M. Sanghavi

Week 5-1: ADT Design

PASS4TEST IT 인증시험덤프전문사이트

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

CS1150 Principles of Computer Science Objects and Classes

Methods. Eng. Mohammed Abdualal

CIS 110: Introduction to computer programming

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Reading Input from Text File

ECOM 2324 COMPUTER PROGRAMMING II

1.00 Lecture 13. Inheritance

Java Object Oriented Design. CSC207 Fall 2014

CMSC 132: Object-Oriented Programming II. Inheritance

Object Oriented Programming

Getter and Setter Methods

Notes on Chapter Three

What is Inheritance?

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

King Abdulaziz University Faculty of Computing and Information Technology Computer Science Department

CSC 615 FINAL EXAM SINGLE PAGE APPS. 1. Introduction

Object Oriented Programming

CS-202 Introduction to Object Oriented Programming

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

Questions Answer Key Questions Answer Key Questions Answer Key

Computer Programming: C++

Access and Non access Modifiers in Core Java Core Java Tutorial

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #9. Review is-a versus has-a. Lecture #9 Inheritance I

Computer Programming, I. Laboratory Manual. Final Exam Solution

Classes, objects, references, encapsulation and whatnot

Classes, objects, references, encapsulation and whatnot

Informatik II (D-ITET) Tutorial 6

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2017 Instructors: Bill & Bill

Chapter 5 Object-Oriented Programming

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

Eng. Mohammed S. Abdualal

Person class. A class can be derived from an existing class by using the form

Packages and Information Hiding

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Object Oriented Programming

JAVA: A Primer. By: Amrita Rajagopal

ENCAPSULATION AND POLYMORPHISM

Overview of Eclipse Lectures. Module Road Map

Chapter 2: Java OOP I

Person class. A class can be derived from an existing class by using the form

Industrial Programming

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

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

Object-Oriented Programming in Java. Topic : Objects and Classes (cont) Object Oriented Design

Object-Oriented Programming in Java

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

5. PACKAGES AND INTERFACES

Chapter 5: Classes and Objects in Depth. Information Hiding

A First Object. We still have another problem. How can we actually make use of the class s data?

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

PART 1. Eclipse IDE Tutorial. 1. What is Eclipse? Eclipse Java IDE

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

coe318 Lab 1 Introduction to Netbeans and Java

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Inheritance. For example, to zoom in on the deer family (cervid), we could make a tree like the following.

Boaz Kantor Introduction to Computer Science IDC Herzliya ( Reichman )

INTERFACES. 24-Dec-10 INTERFACES VS. INHERITANCE. Boaz Kantor Introduction to Computer Science IDC Herzliya ( Reichman ) Interfaces: Inheritance:

Unit 4 - Inheritance, Packages & Interfaces

CIS 110: Introduction to Computer Programming

Outline. CIS 110: Introduction to Computer Programming. Any questions? My life story. A horrible incident. The awful truth

Inheritance and Polymorphism

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Questions Answer Key Questions Answer Key Questions Answer Key

CS 113 MIDTERM EXAM 2 SPRING 2013

Programming overview

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

COMP Information Hiding and Encapsulation. Yi Hong June 03, 2015

Objects and Classes. Lecture 10 of TDA 540 (Objektorienterad Programmering) Chalmers University of Technology Gothenburg University Fall 2017

Create a Java project named week10

Answer ALL Questions. Each Question carries ONE Mark.

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Name:... ID:... class A { public A() { System.out.println( "The default constructor of A is invoked"); } }

Chapter 4: Writing Classes

CSCE 156 Computer Science II

Data Structures (list, dictionary, tuples, sets, strings)

1Z0-808 oracle. Number: 1Z0-808 Passing Score: 800 Time Limit: 120 min.

Object Oriented Programming

CS 1302 Chapter 9 (Review) Object & Classes

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

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

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2018 Instructors: Bill & Bill

CS100J Prelim I, 29 Sept. 2003

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces

Class, Variable, Constructor, Object, Method Questions

Transcription:

Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Created by Eng: Mohammed Alokshiya Modified by Eng: Mohammed Abdualal Lab 11 Object Oriented Programming (II) Eng. Mohammed Abdualal November 29, 2015

Static Variables and Methods The data field age in the Person class is known as an instance variable. An instance variable is tied to a specific instance of the class; it is not shared among objects of the same class. For example, suppose that you create the following objects: Person person1 = new Person("Mohammed", "Ahmed", 23, 120191379); Person person2 = new Person("Ahmed", "Khaled", 19, 120140000); The age in person1 is independent of the age in person2 and is stored in a different memory location. Changes made to person1 s age do not affect person2 s age, and vice versa. If you want all the instances of a class to share data, use static variables, also known as class variables. Static variables store values for the variables in a common memory location. Because of this common location, if one object changes the value of a static variable, all objects of the same class are affected. Java supports static methods as well as static variables. Static methods can be called without creating an instance of the class. Let s modify the Person class by adding a static variable counter to count the number of person objects created. When the first object of this class is created, counter is 1. When the second object is created, counter becomes 2. The constructor methods are guaranteed to be invoked once when creating a new object, so we increment the counter in the constructors. class Person { // Fields String fname; String lname; int age; int ID; static int counter = 0; class Person Static field // A Constructor public Person() { counter++; Increment the counter when creating a new object using first constructor // Another Constructor public Person(String fname, String lname, int age, int ID) { 2

this.fname = fname; this.lname = lname; this.age = age; this.id = ID; counter++; Now the counter field will be incremented when you create any object of class Person. To print the value of counter, get it with the class Name: System.out.println(Person.count); Increment the counter when creating a new object using second constructor You can also define static methods. Static methods can be invoked with class name, without creating objects. An instance method can invoke an instance or static method and access an instance or static data field. A static method can invoke a static method and access a static data field. However, a static method cannot invoke an instance method or access an instance data field, since static methods and static data fields do not belong to a particular object. The relationship between static and instance members is summarized in the following diagram: public static int getcounter() { return counter; Static Method 3

Visibility Modifiers The visibility (access) modifiers in java specifies accessibility (scope) of a data member, method, constructor or class. There are 4 types of java access modifiers: private default protected public Private access modifier: The private access modifier is accessible only within class. In the following example, we have created two classes A and Simple. A class contains private data member and private method. We are accessing these private members from outside the class, so there is compilation error. class A { Simple.java private int data = 40; private void msg() { System.out.println("Hello java"); public class Simple { public static void main(string args[]) { A obj = new A(); System.out.println(obj.data); // Compilation Error obj.msg(); // Compilation Error Default access modifier: If you do not use any modifier, it is treated as default by default. The default modifier is accessible only within package. In the following example, we have created two packages pack and mypack. We are accessing the msg() method in obj object, which is an instance of A class from outside its 4

package, since msg() method is not public, so it cannot be accessed from outside the package. A.java package pack; public class A { void msg() { System.out.println("Hello"); B.java package mypack; import pack.a; public class B { public static void main(string args[]) { A obj = new A(); obj.msg();// Compilation Error In the above example, the scope of method msg() is default so it cannot be accessed from outside the package. Protected access modifier: Protected access modifier will be explained later, when you start working with inheritance and super classes. Public access modifier: The public access modifier is accessible everywhere. It has the widest scope among all other modifiers. Example: A.java package pack; public class A { public void msg() { System.out.println("Hello"); B.java package mypack; import pack.a; public class B { public static void main(string args[]) { A obj = new A(); obj.msg(); 5

Hello Output Summary: Visibility Modifiers Summary Modifier Class Package Subclass World + + + + public y y y y + + + + protected y y y n + + + + no modifier y y n n + + + + private y n n n y: accessible n: not accessible Subclass and protected modifier will be explained in details in Java II. 6

Encapsulation The whole idea behind encapsulation is to hide the implementation details from users. If a data member is private it means it can only be accessed within the same class. No outside class can access private data member (fields) of other class. However if we setup public getter and setter methods to update (for e.g. void setage(int age)) and read (for e.g. int getage()) the private data fields then the outside class can access those private data fields via public methods. This way data can only be accessed by public methods thus making the private fields and their implementation hidden for outside classes. That s why encapsulation is known as data hiding. Lets see an example to understand this concept better. class Person { Person.java // Fields private String fname; private String lname; private int age; private int ID; private static int counter = 0; // Only Getter Method for static field counter // to guarantee that there is no way to change its value // except creating an instance public static int getcounter() { return counter; // A Constructor public Person() { counter++; // Another Constructor public Person(String fname, String lname, int age, int ID) { this.fname = fname; this.lname = lname; this.age = age; this.id = ID; counter++; // Getters and Setters Methods public String getfname() { return fname; 7

public void setfname(string fname) { this.fname = fname; public String getlname() { return lname; public void setlname(string lname) { this.lname = lname; public int getage() { return age; public void setage(int age) { this.age = age; public int getid() { return ID; public void setid(int ID) { this.id = ID; 8

Netbeans Tips & Tricks Code Generation Right click on Edit pane and select Insert Code, or simply press (ALT + INSERT) Then choose what you want to generate. For example, generate Setters and Getters for the class fields: 9

A new window will appear to let you choose the fields that you want to generate setters and getters for them. Moreover, you can use the same window to auto-encapsulate the fields: After you click generate, a new methods will be generated in the class: 10

You can also use Code Generator to generate other methods, like a new constructor(s): 11

Result: 12