25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

21-Aug-14 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
CIT 590 Intro to Programming Java lecture 3. Hashmaps The equivalent of python dictionaries. With both ArrayLists and Hashmaps, the syntax only allows.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
Road Map Introduction to object oriented programming. Classes
Introduction to Programming with Java, for Beginners Intro OOP with Java Java Program Structure.
Access to Names Namespaces, Scopes, Access privileges.
Starting Classes and Methods. Objects have behaviors In old style programming, you had: –data, which was completely passive –functions, which could manipulate.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
18-Jun-15 Arrays. 2 A problem with simple variables One variable holds one value The value may change over time, but at any given time, a variable holds.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
20-Jun-15 Methods. Complexity The programmer's biggest adversary is complexity The programmer's biggest adversary is complexity Primitive types and the.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements You execute those declarations and statements by calling the.
26-Jun-15 Arrays. 2 A problem with simple variables One variable holds one value The value may change over time, but at any given time, a variable holds.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
30-Jun-15 Static Methods. About methods A method is a named group of declarations and statements You execute those declarations and statements by calling.
Access. Overview Today we will discuss: –How do we access fields and methods? –Why have access restrictions? –What can have access restrictions? –How.
16-Jul-15 Access. 2 Overview Questions covered in this talk: How do we access fields and methods? Why have access restrictions? What can have access restrictions?
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
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.
What is inheritance? It is the ability to create a new class from an existing class.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
1 Programming Week 2 2 Inheritance Basic Java Language Section.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Basic Object-Oriented Concepts – towards implementation CS3340.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
OOP Basics Classes & Methods (c) IDMS/SQL News
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
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.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
Objects as a programming concept
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Java Primer 1: Types, Classes and Operators
Namespaces, Scopes, Access privileges
Static Methods 14-Nov-18.
Class Structure 16-Nov-18.
CSC 113 Tutorial QUIZ I.
Class Structure 28-Nov-18.
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
Java Inheritance.
Namespaces, Scopes, Access privileges
Class Structure 25-Feb-19.
Java Programming Language
Classes and Methods 15-Aug-19.
Presentation transcript:

25-Jun-15 Starting Classes and Methods

Objects have behaviors In old style programming, you had: data, which was completely passive functions, which could manipulate any data In O-O programming, an object contains both data and methods that manipulate that data An object is active, not passive; it does things An object is responsible for its own data But it can expose that data to other objects

Objects have state An object contains data The data represent the state of the object Data can also describe the relationship of the object to other objects Example: a checkingAccount might have A balance (the internal state of the account) An owner (some object representing a person) An accountNumber (used as an ID number)

Example: a “Rabbit” object You could create an object representing a rabbit It would have data: How hungry it is How healthy it is Where it is And methods: eat, run, dig, hide

Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields that hold the data for each object Constructors that tell how to create a new object of this class Methods that describe the actions the object can perform In addition, a class can have data and methods of its own (not part of the objects) For example, it can keep a count of the number of objects it has created Such data and methods are called static

Defining a class Here is the simplest syntax for defining a class: class NameOfClass { // the fields (variables) of the object // the constructors for the object // the methods of the object } You can put public, protected, or private before the word class Things in a class can be in any order (I recommend the above order)

Defining fields An object’s data is stored in fields (also called instance variables) The fields describe the state of the object Fields are defined with ordinary variable declarations: String name; Double health; int age = 0;

Defining constructors A constructor is code to create an object You can do other work in a constructor, but you shouldn’t The syntax for a constructor is: ClassName ( parameters ) { …code… } The ClassName has to be the same as the class that the constructor occurs in The parameters are a comma-separated list of variable declarations

Parameters We usually need to give information to constructors and to methods A parameter is a variable used to hold the incoming information A parameter must have a name and a type You supply values for the parameters when you use the constructor or method The parameter name is only meaningful within the constructor or method in which it occurs

Example constructor public class Person { String name; int age; boolean male; Person (String aName, boolean isMale) { name = aName; male = isMale; } } Constructor Parameters

Example use of a constructor The constructor looks like: Person (String aName, boolean isMale) {…} aName and isMale are called formal parameters The formal parameters are used to receive values You can construct a new Person like this: Person john = new Person("John Smith", true); "John Smith" and true are called actual parameters The actual parameters are used to give values to the formal parameters You must have the same number of actual parameters as formal parameters, in the same order, and they must have the same types

A problem with names It would be nice if we could say: public class Person { String name; boolean male; Person (String name, boolean male) { name = name ; male = male ; } }

A problem with names And have it mean: public class Person { String name; boolean male; Person (String name, boolean male) { name = name ; male = male ; } }

A problem with names But this is what it would really mean: public class Person { String name; boolean male; Person (String name, boolean male) { name = name ; male = male ; } }

this to the rescue A parameter may have the same name as an instance variable The name always refers to the parameter The keyword this refers to the current object Putting this in front of a name means that the name is a field of this object (it isn't a parameter)

A problem with names—solved! Here is how we do what we want: public class Person { String name; boolean male; Person (String name, boolean male) { this.name = name ; this.male = male ; } }

A typical use of this If you write a constructor with parameters… …and the parameters are used to set fields that have the same meaning… …then use the same names! Person (String name, boolean male) { this.name = name ; this.male = male ; }  In fact, this is the recommended way to do it

Defining a method A method has the syntax: return-type method-name ( parameters ) { method-variables code } Example: boolean isAdult( ) { int magicAge = 21; return age >= magicAge; }

Returning a result from a method If a method is to return a result, it must specify the type of the result boolean isAdult ( … You must use a return statement to exit the method with a result of the correct type: return age >= magicAge; This is for methods only (constructors automatically return a result of the correct type)

Returning no result from a method The keyword void is used to indicate that a method doesn’t return a value void printAge( ) { System.out.println(name + " is " + age + " years old."); return; }  The keyword return is not required in a void method  The method will return automatically when it reaches the end of the method (the final closing brace)

Sending messages We send a message to an object by: Naming the object Naming the method we want to use Providing any needed actual parameters Example: if (john.isAdult ()) { john.printAge(); } john.isAdult() returns a value (subsequently used by the if statement) john.printAge() does not return a value

Putting it all together class Person { // fields String name; int age; // constructor Person(String name) { this.name = name; age = 0; } // methods String getName() { return name; } void birthday() { age = age + 1; System.out.println( "Happy birthday!"); } }

Using our new class Person john; john = new Person("John Smith"); System.out.print (john.getName()); System.out.println(" is having a birthday!"); john.birthday();  Of course, this code must also be inside a class!

null If you declare a variable to have a given object type, for example, Person john; String name;...and if you have not yet assigned a value to it, for example, with john = new Person(); String name = “John Smith";...then the value of the variable is null null is a legal value, but there isn’t much you can do with it It’s an error to refer to its fields, because it has none It’s an error to send a message to it, because it has no methods The error you will see is NullPointerException

Classes form a hierarchy Classes are arranged is a treelike hierarchy There is one class at the top, or root, of the hierarchy, named Object In computer science we draw trees upside-down, with the root at the top Every class except Object has one “parent” class, or superclass Each class is a subclass of its superclass

What is the class hierarchy for? Classes inherit from their superclasses A class has not only its own fields and methods, but also: Every field described in any class above it Every method described in any class above it Classes do not inherit constructors, however Hence, a class may contain much more information than is obvious from the class description

Example of inheritance Example: class Employee extends Person { double hourlyWage; void pay(double hoursWorked) { System.out.println("Pay to the order of: " + name + " $" + hoursWorked * hourlyWage); } } An Employee has a name, an age, an hourlyWage, and birthday and pay methods.

Class variables and methods A class describes the variables and methods belonging to objects of that class These are called instance variables and instance methods A class may also have its own variables and methods These are called class variables and class methods Class variables and class methods are denoted by the keyword static C programmers: same word as in C, different meaning

Why have class variables and methods? Sometimes you want to keep information about the class itself Example: Class Person might have a class variable Population that counts people This would not be appropriate data to keep in each Person ! Sometimes you want to do something relevant to the class as a whole For example, find the average age of a population Sometimes you don’t have any objects For example, you want to start up a program

Example use of a class variable class Person { // fields String name; int age; static int population; // constructor Person(String name) { this.name = name; age = 0; population = population + 1; } // methods String getName() { return name; } void birthday() { age = age + 1; System.out.println( "Happy birthday!"); } }

Vocabulary I class – a description of a set of objects object – a member of a class instance – same as “object” field – data belong to an object or a class variable – a name used to refer to a data object instance variable – a variable belonging to an object class variable, static variable – a variable belonging to the class as a whole method variable – a temporary variable used in a method

Vocabulary II method – a block of code that can be used by other parts of the program instance method – a method belonging to an object class method, static method – a method belonging to the class as a whole constructor – a block of code used to create an object

Vocabulary III parameter – a piece of information given to a method or to a constructor actual parameter – the value that is passed to the method or constructor formal parameter – the name used by the method or constructor to refer to that value return value – the value (if any) returned by a method

Vocabulary IV hierarchy – a treelike arrangement of classes root – the topmost thing in a tree Object – the root of the class hierarchy subclass – a class that is beneath another in the class hierarchy superclass – a class that is above another in the class hierarchy inherit – to have the same data and methods as a superclass

The End