Applying OO Concepts Using Java. In this class, we will cover: Defining classes Defining methods Defining variables Encapsulation Class methods and variables.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Road Map Introduction to object oriented programming. Classes
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.
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Applications in Java Towson University *Ref:
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.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Object Oriented Programming
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
More Object Concepts— Farrell, Chapter 4 Dr. Burns.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Polymorphism l Constructors.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
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.
OOP Basics Classes & Methods (c) IDMS/SQL News
Class Fundamentals BCIS 3680 Enterprise Programming.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Topic: Classes and Objects
More Sophisticated Behavior
Object-Oriented Programming: Classes and Objects
Methods Chapter 6.
CMSC 202 Static Methods.
Corresponds with Chapter 7
Chapter 6 Methods: A Deeper Look
Object Oriented Programming in java
Programs and Classes A program is made up from classes
Applying OO Concepts Using Java
Tonga Institute of Higher Education
Object Oriented Programming in java
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Chapter 7 Objects and Classes
Presentation transcript:

Applying OO Concepts Using Java

In this class, we will cover: Defining classes Defining methods Defining variables Encapsulation Class methods and variables Final instance and class variables Local variables Accessing methods and variables Passing arguments

Defining Classes Classes are defined in Java using the following format: class { } for example: public class Employee { } Access modifiers define what classes can access this class. Valid access modifiers for class are: Public:means all other classes can access this class. Abstract:(we will go over abstract classes later) Final:(we will go over final classes later) If no access modifier is declared, it defaults to public The naming convention for the name of a class (ClassName) states that classes should begin with a capital letter.

Defining Methods Methods are defined in Java using the following format: ( ) { } for example: public boolean checkName (String name) { if (name.equals(“Homer”)) return true; } Valid access modifiers for methods are: Public:available to all other methods and classes Private:available only to other methods in the class. Protected:available only to other methods in the class, it’s children, and other classes in the same package. Static:(we will learn about this later) Naming conventions for methods state that method name should begin with a lower case letter.

Defining Variables Variables are defined in Java using the following format : ; e.g. public String name; private int count; Valid access modifiers for fields are: Public:available to all methods and classes Private:available only to methods in the class. Protected:available only to methods in the class, it’s children, and other classes in the same package. Static:(we will learn about this later) Naming conventions for methods state that method name should begin with a lower case letter.

Encapsulation In general, classes are usually public to promote reuse. In a public class, it is generally a good idea to make: variables private and methods public. This is the idea behind Encapsulation. Encapsulation refers to the idea of information hiding. -allows other classes to reuse code by just calling a method with the appropriate arguments -no need to know the inner workings of the method -no need to reinvent the wheel

Class Methods and Variables vs. Instance Methods and Variables Instance variables: –variable that is associated with an instance of a class and is associated with an object uniquely –e.g. employeeId Instance methods: –execute within the context of an instance –affect only that particular object

Class Methods and Variables vs. Instance Methods and Variables Class variable (or static field): –is a variables that is associated with a class and is shared by all instances of a class –exist even if no instances of the class exist –declared with the static keyword –e.g. nextAvailableId Class methods (or static methods): –exist even if no instances of class exist –declared with the static keyword –cannot refer to instance variables –useful for utility methods as in the Math class –e.g. main() method is a class method

Final Instance and Class Variables Variables declared as final never change their value. They are constants. A final instance variable is set when an object is created and doesn’t change. A final class (or static) variable is set at the class level before any instances are created. Naming convention for final static variables is in all caps. Example of final variables: public class Employee { // max is a final class (or static) variable public static final INCHES_PER_FOOT=12; // name is a final instance variable private final String name; } }

Local Variables Local variables are variables defined within a method. No access modifier is needed. They can only be referenced within that method. Unlike class and instance variables, they are not initialized automatically. For example, class and instance numeric variables are initialized to 0 or 0.0 Example of local variables: public class Employee { …. public String addToString(String s) { // s2 is a local variable that can only be // referenced in this method String s2 = “ end of string” return s + s2; } }

Accessing Methods and Variables the dot operator –Math.random(); –String.length(); e.g. String name = “Cartman”; int lengthOfName = name.length(); class methods and variables are referenced via the class: –ClassName.methodName() e.g. Math.random() –ClassName.fieldName e.g. Math.PI instance methods and variables are referenced via the instance –InstanceName.methodName() e.g.myString.length() –InstanceName.fieldName() e.g.dice.sides

Accessing Methods and Variables References to variables and methods within the same class do not need to use the ClassName prefix. You can use the “this” reference or leave it blank. The “this” reference is always there implicitly. e.g. public class Employee { int empNumber = 22; public int getEmpNumber( ) { return this.empNumber; } } is the same as public class Employee { int empNumber = 22; public int getEmpNumber( ) { return empNumber; } }

Passing Arguments pass-by-reference vs. pass-by-value pass-by-value: referes to the way in which the 8 basic data types are passed into a method pass-by-reference: refers to the way in which objects are passed into a method See for an example.