Non-Static Classes What is the Object of this lecture?

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
What is inheritance? It is the ability to create a new class from an existing class.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal
Introducing Objects. Structure  Objects have two parts: Instance Variables (attributes, adjectives) Instance Variables (attributes, adjectives) private.
A Revamping Of our skills so far. Our Tools so Far Variable - a placeholder for a value Method - a set of code which accomplishes a task Class - a mold.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
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.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
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.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Liang, Introduction to Java Programming, Sixth Edition1 Objects and Classes Gang Qian Department of Computer Science University of Central Oklahoma.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
CS1054: Lecture 11 More Sophisticated Behavior (contd..)
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
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.
Java Classes Introduction. Contents Introduction Objects and Classes Using the Methods in a Java Class – References and Aliases Defining a Java Class.
 An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Classes (Part 1) Lecture 3
Objects as a programming concept
Implementing Classes Yonglei Tao.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
PHP Classes and Objects
Creating Your OwnClasses
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.
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Pass by Reference, const, readonly, struct
An Introduction to Java – Part II
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Implementing Non-Static Features
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
PreAP Computer Science Quiz Key
Object Oriented Programming Review
JAVA CLASSES.
CIS 199 Final Review.
Chapter 9 Objects and Classes
OO Programming Concepts
Final Exam Review Inheritance Template Functions and Classes
Presentation transcript:

Non-Static Classes What is the Object of this lecture?

Non-Static Methods We have made our own static methods, now it is time to make your own non-static methods. In other words, we will make our own objects and methods which operate upon them. Like Random rand = new Random( ); rand.nextInt( );

Constructors Constructors are basically methods which create objects. You can recognize a constructor because it has the same name as the class it is in. This is because it constructs an object of that class.

Constructor Syntax public class Name { public Name( ) { … code…. }

Object Properties The first thing you must decide is what properties your object will have: Let’s say we have a class called Name Maybe a Name object would have the properties of first and last names (both Strings) There should be a variable for each property

class Name public class Name { private String first, last; public Name( ) { first = “Joe”; last = “Schmoe”; }

Overloading Constructors Remember that methods can be overloaded as long as the parameters are different. The same is true for Constructors The last example was an empty constructor, but we can allow objects to be constructed with the properties specified.

Overloaded class Name public class Name { private String first, last; public Name( ) {first = “Joe”; last = “Schmoe”; } public Name(String theFirst, String theLast) { first = theFirst; last = theLast; }

Non-Static Methods for Name public String getFirst( ) { return first; } public String getLast( ) { return last; } public String getWholeName( ) { return first + “ ” + last; }

Non-Statics cont. public void setFirst(String aName ) { first = aName; } public void setLast(String aName ) { last = aName; } public void setWholeName(String aName1,String aName2) { first = aName 1; last = aName2; }

Mutator Methods Mutator methods change a property of the object which they operate upon. These are very commonly “set” methods, as they assign properties to objects. setFirst( ) of the Name class and showMessageDialog( ) are good examples. Mutator methods are almost always void type

Accessor Methods Accessor methods return information about objects without altering them. These are very commonly “get” methods as they obtain information about objects. getFirst( ) of the Name class and length( ) of the String class are good examples. It is common for accessors to have no parameters

Using Objects Class objects are created in other classes. (Usually one with a main method.) You have done this with the Random class: Random rand = new Random( ); rand.nextInt( ); Calls the constructor Mutator method

Using a Name Object Name ballPlayer = new Name( ); Name president = new Name(“Teddy”, “Roosevelt” ); ballPlayer.setWholeName(“Ricky”, “Henderson”); System.out.println(president.getFirst( )); System.out.println(ballPlayer.getLast( )); Displays: Teddy Displays: Henderson

Variable Specifiers It is important that you make the class instance variables(object properties) private. Private means that no other class has access to them. Imagine you had a Bank account object had an int instance variable called balance: If you made the variable public, any other class could modify your balance!!!

Specifiers cont. The keyword final makes a variable constant. This means that it is not a variable- it cannot change values. Because of this, it is O.K. make public final instance variables… Since they cannot be changed by their own class, no other class can change them either.

Method Access Methods should generally be declared public since you want other classes to be able to use them. If a method is only intended to be used by it’s own class, then it can be private.