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

Similar documents
Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Lesson 8 Passing Parameters. By John B. Owen All rights reserved 2011, revised 2014

Lesson 1A - First Java Program HELLO WORLD With DEBUGGING examples. By John B. Owen All rights reserved 2011, revised 2015

Lesson 7A Arrays. By John B. Owen All rights reserved 2011, revised 2014

Lesson 7B Array methods. By John B. Owen All rights reserved 2011, revised 2014

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

9/17/2018 Programming Data Structures. Encapsulation and Inheritance

Chapter 4 Defining Classes I

Programming Exercise 14: Inheritance and Polymorphism

Lesson 4B Conditionals switch case. By John B. Owen All rights reserved 2011, revised 2014

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

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

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

Dot and Scope Resolution Operator

Lesson 6E Nested Loops. By John B. Owen All rights reserved 2011, revised 2014

CLASSES AND OBJECTS IN JAVA

What is an algorithm?

ECE 122. Engineering Problem Solving with Java

Encapsulation. Mason Vail Boise State University Computer Science

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

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

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

TaxiBot New attributes Variables Math! TaxiBot

Object Orientated Analysis and Design. Benjamin Kenwright

JAVA MOCK TEST JAVA MOCK TEST II

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

Chapter 4. Defining Classes I

Chapter 6 Introduction to Defining Classes

CS112 Lecture: Defining Instantiable Classes

Lesson 5C MyClass Methods. By John B. Owen All rights reserved 2011, revised 2014

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

Chapter 7. Inheritance

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

Programming II (CS300)

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Java Object Oriented Design. CSC207 Fall 2014

What is Inheritance?

Principles of Object Oriented Programming. Lecture 4

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

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

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

Topic 10: Introduction to OO analysis and design

Software Design and Analysis for Engineers

What is an algorithm?

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

Lecture 7: Data Abstractions

Exam Duration: 2hrs and 30min Software Design

UNDERSTANDING CLASS DEFINITIONS CITS1001

Programming II (CS300)

Inheritance and Interfaces

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

CSIS 10B Lab 2 Bags and Stacks

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Inheritance, Polymorphism, and Interfaces

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Lesson 12: OOP #2, Accessor Methods (W03D4)

Inheritance and Polymorphism

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

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

Informatik II Tutorial 6. Subho Shankar Basu

Informatik II (D-ITET) Tutorial 6

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

CompuScholar, Inc. 9th - 12th grades

Chapter 4. Defining Classes I

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

Chapter 15: Object Oriented Programming

Object Oriented Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

CPS122 Lecture: Defining a Class

Object Oriented Programming. C++ 6 th Sem, A Div Ms. Mouna M. Naravani

Chapter 6 Structures and Classes. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Aggregation. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

EECS168 Exam 3 Review

TeenCoder : Java Programming (ISBN )

Anatomy of a Class Encapsulation Anatomy of a Method

Introduction to Inheritance

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Chapter 4: Writing Classes

COMP-202. Objects, Part III. COMP Objects Part III, 2013 Jörg Kienzle and others

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

EECS 1001 and EECS 1030M, lab 01 conflict

Introduction to OOP. Procedural Programming sequence of statements to solve a problem.

Object Model. Object Orientated Analysis and Design. Benjamin Kenwright

Object Oriented Programming with Java

Fundamentals of Object Oriented Programming

Defining Classes and Methods

OBJECT ORİENTATİON ENCAPSULATİON

CS1004: Intro to CS in Java, Spring 2005

Week 7 - More Java! this stands for the calling object:

Transcription:

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

Table of Contents Objectives Encapsulation Inheritance and Composition is a vs has a Polymorphism Information Hiding Public vs Private Access methods Lesson Summary / Labs Contact Information for supplementary materials

Objectives In this lesson we will expand the design of the Student class, and will begin to explore the three primary aspects of OOP, namely Encapsulation Inheritance Polymorphism

The OOP Tri-pod As mentioned in the last slide, the three legs on which Object Oriented Programming stand are: Encapsulation Inheritance Polymorphism

Encapsulation This first leg of OOP refers to the all-encompassing nature of how a class is designed, including the data of the object, as well as any methods it needs.

Encapsulation Abstraction and information hiding are often related to this aspect of designing OOP classes. Both refer to the idea that the user of the class may not always be aware of the inner workings of the class, but is still able to use it.

Encapsulation In the Student class previously explored, the name and age instance fields, as well as three methods - the two constructors and the tostring method - are the significant parts of the encapsulation for this class.

Student class

Inheritance This second leg of OOP design refers to the fact that classes inherit features from previously defined classes, most significantly the Object class, which we explored earlier.

Inheritance We discussed and demonstrated earlier how five of the Object class methods work clone, equals, hashcode, finalize, and tostring and how the tostring method could be customized for the Student class.

Inheritance In the next lesson we will explore this further when we create a Person class, which will serve as a super class for the Student class. We will then design an Employee class which will also inherit the Person class.

Composition This aspect of OOP is closely related to Inheritance and Encapsulation, and refers to the process of building a class that uses other, pre-defined class as components of the class.

Is A vs Has A The key distinction between Inheritance and Composition is easily understood by using the phrases is a and has a.

Is A vs Has A For example, if a Student class inherits a Person class, the Student class is a Person. This is an example of Inheritance.

Is A vs Has A If a Student uses another class, say a Grade class as a part of its data, then it can be said that the Student class has a Grade. This is an example of Composition.

Polymorphism This aspect of OOP design refers to how methods in a class can take many forms, an idea explored earlier when two constructors were designed for the Student class, a default constructor, and another two-parameter constructor. This is called over-loading.

Polymorphism We also customized the tostring method inherited from the Object class in order to provide a better output for the Student class. This is called over-riding.

Information Hiding Let s now discuss the feature of Encapsulation where information from a class can be hidden, or protected from the outside world, allowing access to it only in a controlled way.

Information Hiding This aspect of OOP design is crucial since most data is sensitive and important to protect, such as bank account balances, passwords, private information, medical records, etc.

Information Hiding To make data secure, we use the Java reserved word private in front of each data item, which allows ONLY that particular object, or other member objects of that class, access to that data. Let s look at this with our sample program.

Public access In the Student class we defined earlier, there were two instance fields, name and age.

Public access They are public by default, even if the word public is not included, as was the case in the original example (see slide 8).

Public access Public access simply means they can be accessed directly from any program portion external to the class definition.

Public access This public data can be accessed from main directly, a static method that belongs to the class, but not directly to an object.

Public access By simply stating me.name and me.age, the data in this object can be directly accessed, as you can see in the output statements below.

Public access Further evidence is shown below, where direct changes are made to the name and age fields.

Public access It stands to reason that this is NOT a good thing, especially for sensitive data.

Private access To fix this, public is changed to private. This should result in a compile error, but doesn t. Hmm???

Private access Here s why since main is a member of this class definition, it can see all of the private data Hmm???

Private access main is now in a separate class, no longer a member of the Student class.

Private access The result is several compile error messages, as you can see below.

Special access methods The proper way to access these private data fields is by using special methods called accessors and modifiers (mutators) These methods are often named using the prefixes set and get. Study the code on the next slide carefully.

Accessor methods get

Accessor methods get The get methods shown are simple in nature, and may seem like extra work, but are crucial to the information hiding process.

Accessor methods get The format is simple Start with the word public then the type of data

Accessor methods get then a method name that starts with the word get, followed by the capitalized name of the field, with an empty parentheses at the end.

Accessor methods get The only statement required is a return statement with the name of the field.

Accessor methods get If desired, extra access protection could be provided inside this method by requiring some kind of password value through the parameter list.

Modifier, or mutator methods set On the next slide see examples of modifier methods.

Modifier, or mutator methods set

Modifier, or mutator methods set This format is simple, but different Start with the word public then the word void this is NOT a return method it just performs an action

Modifier, or mutator methods set then the method name, starting with the word set, and the capitalized name of the field.

Modifier, or mutator methods set The formal parameter must match the type of data, and can be named anything, usually an abbreviation of the matching instance field.

Modifier, or mutator methods set It is NOT a good idea to use exactly the same name as the field, as this provides some confusion and requires extra code to clarify the scope. Don t do it! It gets messy!

Modifier, or mutator methods set The only statement required is the assignment statement that gives the value of the parameter to the instance field.

Lesson Summary In this lesson, you learned all about the three main aspects of OOP, namely Encapsulation, Inheritance, and Polymorphism You also explored the notion of public vs private, and how to create access methods.

Lab Create a Tester class as shown in this lesson and move the main method to that class. Using the Student class, add one more private instance field called gpa, which will be a decimal value. Adjust the two constructors and the tostring method to include the new field

Lab Write the get and set method for the new field. Expand the main method to include access to this new field, in the same way as the two other fields are accessed and modified.

CONGRATULATIONS! Now you know how to protect the private data in your program by using access methods in your class definition. Now move on to the next lesson as we further explore the aspect of Inheritance.

Thanks, and have fun! To order supplementary materials for all the lessons in this series, including lesson examples, lab solutions, quizzes, tests, and unit reviews, visit the O(N)CS Lessons website, or contact me at John B. Owen captainjbo@gmail.com