Session 7 Methods Strings Constructors this Inheritance.

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
Lecture 2 Classes and objects, Constructors, Arrays and vectors.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
From C++ to C#. Web programming The course is on web programming using ASP.Net and C# The course is on web programming using ASP.Net and C# ASP.Net is.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
The Rest of the Story.  Constructors  Compiler-generated  The Initializer List  Copy Constructors  Single-arg (conversion ctors)  The Assignment.
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.
Utilities (Part 2) Implementing static features 1.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
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.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC 142 Computer Science II Zhen Jiang West Chester University
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
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.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
More About Objects and Methods
Java Review: Reference Types
CSC240 Computer Science III
Object Based Programming
Interface.
Object Oriented Programming in java
Today’s topics UML Diagramming review of terms
Unit-1 Introduction to Java
Classes and Objects Object Creation
Corresponds with Chapter 5
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Session 7 Methods Strings Constructors this Inheritance

Java® Methods Methods are the interface or communications between classes –They provide a way to invoke the same operation from many places in your program, avoiding code repetition –They hide implementation details from the caller or user of the method –Variables defined within a method are not visible to callers of the method; they have local scope within the method. –The method cannot see variables in its caller either. There is logical separation between the two, which avoids variable name conflicts.

Invoking a Method

Passing Arguments void main(String[ ] args){… double a; int x; b= power(a, x); double power(double d, int i) {// Method makes own copy // of d and i implicitly double x= d; … return x;} Argument 1Argument 2 Return value Communi-cation only via arg list, return value Arguments matched by position Assume method is written first: can’t know main() vars

Call By Value Java® supports only call-by-value when passing arguments to a method: –The method makes a local copy of each argument –It then operates on that local copy –A method cannot change the value of an argument (variable) in the method that calls it

Call By Value –A method can only send information back to its caller Via the return value, and By changing the data in objects passed to it (more on this later) –The return value can be void, meaning nothing is returned. Method main() has a void return. –The other mechanism, supported by C++ and other languages, is call-by-reference, which allows the method to change the arguments.

Call By Value Example

Call By Value With Object

Call by Value With Object

Method Signature The name and list of arguments of a method is called its “signature” Within a class, methods can have same name as long as they have different signature Within a class, all methods with same name must have same return type.

Method Overloading Using same method name with different signatures is called “method overloading”. Java® selects which version of overloaded method to call based on number, type and order of arguments in the method’s invocation Java® will “promote” numerical data types “upwards” to match signatures

String class Part of Java® system String class has special operator for concatenation String class allows constant initializer, as in String testString = “Hello”; String is not modifiable once created- ”immutable”

Strings

“this”- How an object refer to itself Objects are accessed by variables we called references. Suppose from code within an object, we want to refer back to the object. Example: Suppose we want to pass a reference to “ourself” as argument to a method of another object

this “this” is also used as shorthand to refer to other constructors for the class

this in a constructor

Springs F= k dx

Spring Class Constructors

Spring Class Methods

Spring Class Design All the Spring methods are public –Any method of any class can call these methods –Private methods can be used, as helpers or for tricky things that only the class itself should do Data fields in Spring are private –Only Spring methods can access them –Public data fields are almost never used Constructor name must be same as class name –Constructors are called with new only –Constructors cannot have a return value

Spring Class Methods Why have all these methods? –Get methods are only access from other classes to Spring data Allow us to reimplement Spring if we need –Set methods should do error checking Our method should make sure that length>0, max deflection < length, etc. Overloaded methods must have different signatures (arguments) –Different return types cannot distinguish two otherwise identical methods int getForce(int a)and double getForce(int b)would not compile

Object Destruction Java® reclaims object memory automatic- ally using ‘garbage collection’ when there are no active references to the object –C++ requires the programmer to do this manually. You use ‘new’ sparingly in C++ because you have to use ‘delete’ when done, to avoid ‘memory leaks’ Java® has finalizers to clean up other resources (files, devices locked, etc.) when an object is destroyed –Informal advice: never use finalizers –They can invoke any object, so garbage collector can be wildly inefficient

Beam Exercise Write a Java® class to model this beam and compute its maximum deflection: w= - P L3/ 3 E I where P= load at end (1200 N) L= length of beam (20 m) E= elastic modulus (30000 N/ m2) I= moment of inertia (1000 m4 ) w= deflection (m)

Beam Exercise, p.2 Data fields: –Which are beam characteristics? –Which are external? Write two constructors: –One with all fields as arguments Use same names for arguments and fields –One with only length argument Other fields default as on previous slide Use ‘this’ to invoke other constructor or rely on initialization –Use initialization in your class: assume you’re dealing with many beams like the example beam Write two methods to return the deflection w –Use same method name for both (overloading) –One takes load as argument, 2nd takes load and units (ft or m) as a String; convert result using 1 m= 3.3 ft Don’t write any get or set methods Write a class with a main() to create a beam, compute its deflection and print the result

Beam Exercise, p.3 Optional, advanced: –Add dimensional analysis: Store the units for each variable in the class –Decide how you’ll code them ( exponents, etc.) Modify constructors to take unit arguments Convert units if needed (N – lbf, m – ft) –1 lbf= 4.4 N, 1 ft= 0.3 m Make sure units match in the calculation Output the units with the method result

Beam Exercise, p.2 Data fields: –Which are beam characteristics? –Which are external? Write two constructors: –One with all fields as arguments Use same names for arguments and fields –One with only length argument Other fields default as on previous slide Use ‘this’ to invoke other constructor or rely on initialization –Use initialization in your class: assume you’re dealing with many beams like the example beam Write two methods to return the deflection w –Use same method name for both (overloading) –One takes load as argument, 2nd takes load and units (ft or m)as a String; convert result using 1 m= 3.3 ft Don’t write any get or set methods Write a class with a main() to create a beam, computeits deflection and print the result

Beam Class

Beam Main()