CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.6 Access Modifiers,

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

Final and Abstract Classes
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Object-Oriented PHP (1)
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 9 Thinking in Objects.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
CS 2430 Day 9. Announcements Quiz on Friday, 9/28 Prog1: see , see me as soon as possible with questions/concerns Prog2: do not add any public methods.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 1-Introduction to Java Topic 1.2 Getting the Tools and Setting Up the Development.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 1-Introduction to Java Topic 1.1 Basics of Java Produced by Harvey Peters, 2008 Copyright.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
ISBN Object-Oriented Programming Chapter Chapter
CPRG 215 Introduction to Object-Oriented Programming with Java Module 4- Exception and Error Handling Topic 4.1 Errors and Exceptions Produced by Harvey.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,
CS 5JA Introduction to Java Pop Quiz Define/Explain encapsulation. In object-oriented programming, encapsulation refers to the grouping of data and the.
Classes, Interfaces and Packages
CPRG 215 Introduction to Object-Oriented Programming with Java Module 1-Introduction to Java Topic 1.3 Write Your First Java Program Produced by Harvey.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.6 Reading Input with java.io Produced by Harvey.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
CPSC 233 Tutorial 12 March 4/5 th, TopHat Quiz int[] a = {0}; int[] b = {1}; a = b; What is the value of a[0] i) 0 ii) 1.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.1 Fundamental Concepts.
Topic: Classes and Objects
OOP: Encapsulation &Abstraction
Examples of Classes & Objects
Objects as a programming concept
Final and Abstract Classes
Chapter 3: Using Methods, Classes, and Objects
8/30/2018 CPRG 215 Introduction to Object-Oriented Programming with Java Module 5- The java.io Package Topic 5.1 Input and Output Streams, Readers.
Object-Oriented Programming Using C++
Corresponds with Chapter 7
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Classes & Objects: Examples
METHOD OVERRIDING in JAVA
Static in Classes CSCE 121 J. Michael Moore.
Java Programming, Second Edition
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Object-Oriented Programming Using C++
Object Oriented Programming
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
ITE “A” GROUP 2 ENCAPSULATION.
CS 240 – Advanced Programming Concepts
CSG2H3 Object Oriented Programming
Presentation transcript:

CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.6 Access Modifiers, static, and final Produced by Harvey Peters, 2008 Copyright SAIT

Please review the following sections in your textbook Core Java, Volume I–Fundamentals, Eighth Edition By Cay S. Horstmann & Gary Cornell Chapter 5 - Inheritance Design Hints for Inheritance CPRG 215 Module Access Modifiers, static, and final Copyright SAIT

Class (static) Variables Are shared among all instances of a class Can be accessed from outside the class if marked as public without an instance (object) of the class public class Count { private int serialNumber; private static int counter = 0; public Count() { counter++; serialNumber = counter; } This means that it doesn’t get created in the object, but stays in the class It can be accessed from other classes and objects by calling the class and the item name Example: Count.serialNumber CPRG 215 Module Access Modifiers, static, and final Copyright SAIT Topic 3.6.1

public class Count { private int serialNumber; private static int counter = 0; public Count() { counter++; serialNumber = counter; } Class (static) Variables CPRG 215 Module Access Modifiers, static, and final Copyright SAIT Topic This means that it doesn’t get created in the object, but stays in the class It can be accessed from other classes and objects by calling the class and the item name Example: Count.serialNumber

Class (static) Methods A static method can be invoked without any instance of the class to which it belongs CPRG 215 Module Access Modifiers, static, and final Copyright SAIT Topic public class GeneralFunction { public static int addUp(int x, int y) { return x + y; } public class UseGeneral { public void method() { int a = 9; int b = 10; int c = GeneralFunction.addUp(a, b); System.out.println(“addUp() gives “ + c); }

final A final class cannot be subclassed public final class MyClass A final method cannot be overridden public final void MyMethod() A final variable is a constant public final int MAX_ARRAY_SIZE = 25; –roughly equivalent to “const” in C and C++ –Can only be initialized during object construction CPRG 215 Module Access Modifiers, static, and final Copyright SAIT Topic 3.6.3

Access Control Data hiding –Cornerstone of Encapsulation –if the data members of a class are not exposed, the class user is forced to use the methods to alter the member variables -- validity checking can be done in the methods! –We hide the data by using access modifiers. CPRG 215 Module Access Modifiers, static, and final Copyright SAIT Topic 3.6.4

Access Control public class MyDate { private int day, month, year; public void tomorrow() { this.day = this.day + 1; // validate day range… } public class DateUser { public static void main(String args[ ]) { MyDate mydate = new MyDate(); mydate.day = 21; //illegal!!! } CPRG 215 Module Access Modifiers, static, and final Copyright SAIT Topic Data hiding

Access Control ____________________________________________ ModifierSameSameSubclassUniverse ClassPackage ____________________________________________ publicYesYesYesYes protectedYesYesYes defaultYesYes privateYes ____________________________________________ Note: default is not a keyword -- it indicates situations where a modifier is not supplied CPRG 215 Module Access Modifiers, static, and final Copyright SAIT Topic 3.6.4