Agenda Warmup Lesson 2.6 (Constructors, Encapsulation)

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Multiple Choice Solutions True/False a c b e d   T F.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Writing Classes (Chapter 4)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Overloading a constructor If a constructor is overloaded, then an if statement in the client is needed when creating the object. We have always declared.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Constructors A constructor is a method that has the same name as the class itself It is automatically called when an object is instantiated (in other words,
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 10 Thinking in Objects
Topic: Classes and Objects
Classes (Part 1) Lecture 3
Agenda Warmup Lesson 1.4 (double precision, String methods, etc)
Phil Tayco Slide version 1.0 Created Sep 18, 2017
Chapter 10 Thinking in Objects
Chapter 10 Thinking in Objects
Interfaces.
Agenda Warmup Finish 2.4 Assignments
Agenda Warmup AP Exam Review: Litvin A2
Chapter 3: Using Methods, Classes, and Objects
Creating Your OwnClasses
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Can perform actions and provide communication
Control Statement Examples
Object Based Programming
Classes and Objects: Encapsulation
Chapter 9 Thinking in Objects
Can perform actions and provide communication
Encapsulation & Visibility Modifiers
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
IFS410: Advanced Analysis and Design
Lecture 22 Inheritance Richard Gesick.
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Can perform actions and provide communication
Unit 3 Test: Friday.
Chapter 9 Thinking in Objects
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Welcome to AP Computer Science A!
Agenda Warmup Lesson 1.6 (Do-while loops, sentinels, etc)
Agenda Warmup Lesson 1.4 (double precision, String methods, etc)
Agenda Warmup Lesson 1.9 (random #s, Boolean variables, etc)
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Agenda Warmup Lesson 2.2 (parameters, etc)
Agenda Warmup Lesson 1.6 (Do-while loops, sentinels, etc)
Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)
Agenda Warmup Lesson 2.8 (Overloading constructors, etc)
CMSC 202 Encapsulation Version 9/10.
Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)
CSG2H3 Object Oriented Programming
Agenda Warmup Lesson 1.9 (random #s, Boolean variables, etc)
Agenda Warmup Lesson 2.4 (String concatenation, primitive types, etc)
Presentation transcript:

Agenda Warmup Lesson 2.6 (Constructors, Encapsulation) Independent Practice (2.6 Assignments) (Time Permitting) Lesson 2.7 Independent Practice (2.7 Assignments) Closure Activity Students will be able to: Understand what a constructor is Create a constructor in a class, as well as call that constructor from a client Understand the concept of encapsulation, and how it affects code See how today's lesson fits into the unit and the course as a whole

Warmup What is an immutable class? 2) Should instance variables ever be public? 3) Can a method be private? 4) Is it possible for a method to receive no parameters, display nothing, and return nothing? String x = “abcdef”; System.out.print(x.substring(4)); System.out.print(x.substring(1, 3)); // result? 6) Which word changes in pronunciation and meaning when you capitalize the first letter?

Constructors A constructor is a method that has the same name as the class itself It is automatically called when an object is declared (in other words, when you “connect to” (instantiate) a class) example: ClassName Q = new ClassName( ); It is used to “set up” the class – usually this means initializing its instance variables, sometimes this means calling other methods Constructors cannot return a value (but they don’t use void either!) They cannot be called like other methods: they are only called when an class is instantiated Example: ConstructorDemoClass & ConstructorDemoClient

Encapsulation Encapsulation is an important concept in object-oriented programming. Essentially, it means that the internal workings of an object should be hidden. Or, in other words, when you (as a programmer) use a class (by creating an object), you should know what the class does, but not necessarily how it does it. When data is restricted by using private variables and methods, this is a concept called information hiding.

Practically speaking, this means simply that almost always, instance variables in a class should be private. Then, if you want to access those variables from a client, the only way to do this should be by calling public accessor methods which return those variables. So, directly accessing a class’s variables violates the concept of encapsulation. (It does happen anyway, sometimes.)

We have already learned that a mutator method sets the value of a private instance variable. One reason for encapsulation is that you can use error checking in a mutator method to validate data. For example, in a class called Student, there is an instance variable, studentIDNumber. If that number must be 6 digits long, this can be enforced in a mutator method. If, instead, studentIDnumber was public, it could potentially be set by the client to an invalid number. See next slide.

public class Student { private String studentIDnumber = 0; public void setIDnumber(String x) while(x.length() != 6) x = SavitchIn.readLine(); studentIDnumber = x; } public String getIDnumber() return studentIDnumber;

Encapsulation also means that even some methods should be private. This is because certain “helper” methods perform tasks that support other methods, and are not meant to be called by a client. Using helper methods is a concept known as procedural abstraction. Demo: EncapsulationDemoClass & EncapsulationDemoClient

IMPORTANT: technically speaking, a helper method is private – meaning that the client does not have access to it. It can ONLY be called by another method in the same class. However, sometimes a public method, meant to be called from a client, can ALSO be called by other methods. It’s not technically a helper method, but it can be used by other methods as if it was. HINT – use this technique for today’s assignment. One of your methods should be called from another method, even though it is public!

Example: System.out.println(objectName); toString( ) method Almost every class should have a toString( ) method. toString() is used to quickly and easily display the most important info from a class toString( ) is called automatically when you put the name of an object inside a System.out.println( ). Example: System.out.println(objectName); You can also call toString( ) like you would call any other method that returns a String, but displaying the object name is the preferred technique. Demo: add on to EncapsulationDemoClass

Assignment Create a class called Mar16Class with the following methods. (Then create Mar16Client to test it.) Create a constructor. It receives the person’s age and favorite donut as parameters, and it assigns these values to instance variables in the class. ageDiv() – accepts no parameters, returns true if the person’s age is divisible by 5, false if not abs() - accepts one number as a parameter, returns the absolute value of that number. Although there is a Math.abs() method, do this instead with an if. nextToLast() - accepts one word as a parameter, returns the next-to-last letter in the word getName() – accepts no parameters; asks for the user’s name, then returns one String, consisting of the name with an insult attached to it ***More on next slide…

displayStuff() – accepts a num and a word, displays that word num+1 times, returns nothing findIndex() – accepts a word and a letter as parameters, returns which index that letter was found at in the word (ignore multiple occurrences of the letter), returns 999 if the letter is not found primeNum() - receives a number, returns true if it is prime, false if not. (make sure to test this!) getPrimes( ) -- Accepts two #s, returns how many primes exist between (and including) the two numbers. sumOfDigits( ) – accepts a 4-digit number, returns the sum of its digits. No Strings allowed. toString( ) – returns the name, age, the favorite donut, and the sum of the 4 digits.