Chapter 3 Introduction to Classes and Objects Definitions Examples.

Slides:



Advertisements
Similar presentations
Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Tutorial 6 February 4th/5th, 2015
Road Map Introduction to object oriented programming. Classes
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Introduction to Java Programming, 4E Y. Daniel Liang.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Packages. Package A package is a set of related classes Syntax to put a class into a package: package ; public class { …} Two rules:  A package declaration.
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.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
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.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Objects and Classes Mostafa Abdallah
© 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.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
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.
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.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Classes, Interfaces and Packages
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
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.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Topic: Classes and Objects
Chapter 3: Using Methods, Classes, and Objects
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Corresponds with Chapter 7
Objects and Classes Creating Objects and Object Reference Variables
Chapter 6 Objects and Classes
Object Oriented Programming in java
OO Programming Concepts
Chapter 6 Objects and Classes
Classes and Objects Systems Programming.
Chapter 7 Objects and Classes
Presentation transcript:

Chapter 3 Introduction to Classes and Objects Definitions Examples

Definitions Object – instance of a class Objects have attributes and behaviors Attributes – data Behaviors – methods or operations

Definitions Encapsulation – combining attributes and methods into one group (object) Information hiding – Implementation details are hidden within the objects themselves Abstraction – another term for information hiding

Definition Default package – set of classes that are compiled in the same directory on disk. We say that all such classes are considered to be in the same package Classes in the same package are implicitly imported into the source code files of other classes in the same package. Import is not required when one class in a package uses another class in the same package.

Definitions Java classes contain – Methods – implement operations – Fields – implement attributes Accessor methods – Methods that allows access to internal data fields Example: getInterestRate() //method name generally starts with “get” //highly recommended that it starts with “get”

Definitions Mutator methods – methods that change values in data fields Example: setInterestRate (double x) //method name generally starts with “set” //highly recommended that mutator methods start //with “set”

Variables Local variables – Variables declared in the body of a particular method Fields – Variables declared inside a class declaration but outside the bodies of the class’s method declarations – Also known as instance variables (if not static)

Fields – instance variables Can be declared as public, private, protected, default Generally should be declared as private to promote data hiding Methods generally declared as public

Example class (Circle.java) public class Circle { private double radius = 1.0;//field double findArea() { return radius * radius * ; }

Declaring a reference to an object and creating an object Declaration: Classname objectReference; Circle myCircle; //myCircle is a reference to a Circle //object Create a Circle object myCircle = new Circle(); Declare and create Circle myCircle = new Circle();

Circle Class CircleClass.java CircleProgram.java

Constructors Method with the same name as the class Allows construction of objects with different initial data values – initializes object Do not have a return type Invoked using the new operator Circle (double r)Circle (){ radius = r;radius = 1;}

Constructors Default constructor – Constructor with no parameters If your class has no constructors, Java will automatically create a default constructor If your class has a constructor, Java will not create a default constructor You should include default constructors in your class.

Instance Variables Normally should be initialized in constructors Alternative: Initialize when declarations are made If instance variables are initialized in the declaration and you have constructors --- Be sure to define a default constructor even if the body of the default constructor is empty. The following: AClass anObject = new AClass(); is illegal if no default constructor but other constructors exist.

AClass anObject = new AClass(); public class AClass { private int value = 1; public AClass (int x) { value = x; } public void setValue(int x) { value = x; } public int getValue() { return value; }

Static Methods – methods that do not require a calling object Uses the keyword static in method heading You cannot do anything inside a static method that refers to a calling object – You cannot access instance variables from inside a static method You cannot call a non-static method inside a static method )unless you first create a new object and use it as the calling object) You can invoke a static method with an object – Confusing and is not normally done.

Method Syntax [access specifier] [qualifier] return type method name (argument list)

Access Specifiers – protecting class variables and methods SpecifierClassSubclassPackageWorld PrivateX ProtectedXXX PublicXXXX Package (default) XX

Static Variables Static variables belong to the class as a whole and not just to one object – private static int turn; Used to communicate between objects One object can change the static variable and another object can read that change Should normally be declared as private (unless declared as constants)

Final Instance Variables Keyword final is used to specify that a variable is not modifiable (it is a constant) private final int INCREMENT = 5; Use all caps when denoting constants

Method arguments All arguments are pass by value – methods always receive a copy of the argument If argument is an object reference (different story)

Class methods – static methods Example – Integer.parseInt(s); – main (String args[]) An instance of the class is not necessary to access the static method Static methods can only call static methods and can only access static variables Method is usable by any code that has access to the method – regardless of whether any objects of the class have been created.