Notes from Week 5 CSE 115 Spring 2006 February 13, 15 & 17, 2006.

Slides:



Advertisements
Similar presentations
Looking inside classes Fields, Constructors & Methods Week 3.
Advertisements

OOP: Inheritance By: Lamiaa Said.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
1 Programming Week 2 James King 12 August Creating Instances of Objects Basic Java Language Section.
Week 4 Recap CSE 115 Spring Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Week 2 Recap CSE 115 – Spring Object Oriented Program System of objects that communicate with one another to solve some problem.
Week 6 Recap CSE 115 Spring Review We have built a program that uses Association, Composition, Instantiation Dependency, Local Instance Dependency,
Week 4 Recap CSE 115 Fall 2006 Section C. Decoupling Separation of concerns Defining what an object can do, not how it does it.
Java Syntax, Java Conventions, CSE 115 Conventions (Part 2) CSE 115 Spring 2006 January 30, February 1 & 3, 2006.
Week 5 Recap CSE 115 Spring Composition Informally called “has a” Represented in UML with a diamond- headed arc In code: Declare an instance variable.
CSE 115 Week 4 February 4 - 8, Monday Announcements Software installation fest Tuesday & Wednesday 4-7 in Baldy 21. Software installation fest Tuesday.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Week 3 Recap CSE 115 – Spring Constructor Special capability of a class that sets up the initial state of the object. Constructor definitions are.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
Week 3 Recap CSE 115 – Fall Java Source Code File Made up of: Package declaration Class definition.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Comments are for people Header comments supply basic information about the artifact.
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.
CSC 142 Computer Science II Zhen Jiang West Chester University
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
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.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
© 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.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Classes, Interfaces and Packages
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
Inheritance.
OOP: Encapsulation &Abstraction
Objects as a programming concept
CS-104 Final Exam Review Victor Norman.
CSC 108H: Introduction to Computer Programming
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
User Defined Functions
Can perform actions and provide communication
Corresponds with Chapter 7
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Inheritance, Polymorphism, and Interfaces. Oh My
Classes, Encapsulation, Methods and Constructors (Continued)
CSE 115 September 29 – October 3, 2008.
Can perform actions and provide communication
CSE 115 September 29 – October 3, 2008.
Java Inheritance.
Chapter 10: Method Overriding and method Overloading
Object-Oriented Programming
Classes CS 21a: Introduction to Computing I
Method Overriding and method Overloading
Chapter 4 Test Review First day
Presentation transcript:

Notes from Week 5 CSE 115 Spring 2006 February 13, 15 & 17, 2006

Writing Methods is a two-step process Give definition of how method will perform – write the method. When we want the work of the method to actually get done – the method call.

Writing the method (defining the method) Specify Method Signature: Visibility Return type Name for method Parameter list Specify Method Body: Code that method must execute to perform required task.

Method Writing - Method Signature Visibility public is best if you want anyone to be able to call your methods. Return type void if nothing is returned. If something is returned, need to use keyword return in method body to indicate value being returned.

Method Writing - Method Signature Give the method a name (mind the rules) Method is an identifier Style: first letter lower case, each subsequent word capitalized. Eg) myFirstMethod

Method Writing - Method Signature Parameters (formal parameters) Specify for each Type Name Why do parameters get a name in the method definition? So we can refer to them inside the method body.

Method Writing - Method Body Code that is executed when method is called. Need additional variables inside the method? Create a local variable. Local variables are only accessible from inside the method body.

Method Calling Method call: instanceName.methodName(); Pass in actual values for each of the required parameters. (actual parameters) Do something with the return type if there is one. (What if you don’t?)

”Strings are surrounded by quotes” List of Characters Characters are any single letter, digit, or symbol In code: ”Strings are surrounded by quotes”

Printing information to the console (screen) System.out.println(”Some text.”);

Inheritance Can be viewed as a specialization of a superclass. Can be viewed as generalizing several subclasses into a superclass.

Generalization Relationship “is a” relationship There is a superclass and a subclass In code: public class ClassB extends ClassA { }

Inheritance Basics When a subclass extends a superclass, the subclass inherits all public capabilities (methods) of the superclass and they remain public in the subclass. When a class has a superclass and we create an instance of the subclass, the no-argument constructor of the superclass is called automatically from the constructor of the subclass.

Accessors & Mutators Methods inside of a class that work with the instance variables to provide the value of the instance variable and to change the value of the instance variable respectfully.

Method Overloading Two methods can have the same name only if they differ in number and/or type of parameters. Constructors can be overloaded too!

Accessors (get methods) Access the value of an instance variable. public TypeOfProperty getProperty(){ return _instanceVariableName; }

Mutators (set methods) Change the value of an instance variable. public void setProperty (TypeOfProperty newValue) { _instanceVariableName = newValue; }