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,

Slides:



Advertisements
Similar presentations
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Object-Oriented PHP (1)
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Enhancing classes Visibility modifiers and encapsulation revisited
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
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.
Writing Classes (Chapter 4)
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
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.
CS125 Exam Review Winter Some Exam Info Tuesday (22nd) at 4:00-6:30pm in the PAC CHECK YOUR SEAT!!! Read Final Exam Information on website –Practice.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Quizard step-by-step. Start with the questions The Question class is abstract –a Superclass You’ll never create something that’s just a question. It’ll.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Abstraction ADTs, Information Hiding and Encapsulation.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating 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.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
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.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
OOP Basics Classes & Methods (c) IDMS/SQL News
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.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Topic: Classes and Objects
Object-Oriented Concepts
Classes (Part 1) Lecture 3
Agenda Warmup AP Exam Review: Litvin A2
Chapter 3: Using Methods, Classes, and Objects
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
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.
Interfaces.
Can perform actions and provide communication
Object Based Programming
Classes and Objects: Encapsulation
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
Classes & Objects: Examples
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Can perform actions and provide communication
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Agenda Warmup Lesson 2.6 (Constructors, Encapsulation)
Agenda Warmup Lesson 2.8 (Overloading constructors, etc)
CMSC 202 Encapsulation Version 9/10.
Chapter 5 Classes.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

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, when you “connect to a class”) example: Param Q = new Param( ); 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 object 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 is by calling public methods which return those variables So, directly accessing a class’s variables violates the concept of encapsulation.

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

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( ). So, you do NOT call toString( ) the same way you call other methods! Demo: add on to EncapsulationDemoClass

Assignment Create a class called DavosClass with the following methods. (Then create a client to test it.) Create a constructor. It receives the person’s age as a parameter, and it assigns this value to an instance variable 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 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 it, with an insult attached to it ***More on next slide…

displayStuff() – accepts a num and a word, displays that word num 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!) getPrimez( ) -- Accepts two #s, returns how many primes exist between (and including) the two numbers. sumOfDigits( ) – accepts a 3-digit number, returns the sum of its digits