CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.

Slides:



Advertisements
Similar presentations
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Road Map Introduction to object oriented programming. Classes
Unit 4II 1 More about classes H Defining classes revisited H Constructors H Defining methods and passing parameters H Visibility modifiers and encapsulation.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
INTRODUCTION TO JAVA PROGRAMMING Chapter 1. What is Computer Programming?
C++ fundamentals.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Programming Languages and Paradigms Object-Oriented Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Writing Classes (Chapter 4)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
An Object-Oriented Approach to Programming Logic and Design
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
The Java Programming Language
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
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 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Introduction to Defining Classes
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object-Oriented Concepts Overview. Florida Community College at Jacksonville COP 2551 Object-Oriented Programming OO Concepts Overview Objective Overview.
Introduction To Java Programming 1.0 Basic Concepts of Java Programming 2.0 Classes, Polymorphism and Inheritance 3.0 Exception handling 4.0.
OOP Basics Classes & Methods (c) IDMS/SQL News
Class Fundamentals BCIS 3680 Enterprise Programming.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 3 Implementing Classes
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Programming Logic and Design Seventh Edition
3 Introduction to Classes and Objects.
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Your Own Classes Part 1
Corresponds with Chapter 7
Week 3 Object-based Programming: Classes and Objects
Problem Solving, Object-Oriented Design and Java
COP 3330 Object-oriented Programming in C++
Object Oriented Programming in java
Classes CS 21a: Introduction to Computing I
Chap 2. Identifiers, Keywords, and Types
Final and Abstract Classes
Presentation transcript:

CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1

CSM-Java Programming-I Lesson-1 Objectives Java Setup Instructions. Overview of Object Orientation. Overview of Objects and Classes. 3 basic principles of OOP (Encapsulation, Inheritance and Polymorphism).

CSM-Java Programming-I Lesson-1 How to Program in Java 1: Edit code using any text editor 2: Compile code using javac ClassName.java Any errors? If yes, repeat 1 and 2; if no, continue 3: Run Java application java ClassName Any errors? If yes, repeat step 1, 2 and 3.

Overview of Object Orientation An Object-Oriented Program consists of a group of cooperating objects, exchanging messages, for the purpose of achieving a common objective. Object: An entity that you can manipulate in your programs (by invoking methods). It has: state - attributes behaviors - operations or methods CSM-Java Programming-I Lesson-1

Overview of Object Orientation Example: A particular bank account has an account number has a current balance can be deposited into can be withdrawn from CSM-Java Programming-I Lesson-1

Classes Classes provide the structure for objects. A class defines the methods and types of data associated with an object. Creating an object from a class is called instantiation; an object is an instance of a particular class For example, the Account class could describe many bank accounts, but toms_savings is a particular bank account with a particular balance CSM-Java Programming-I Lesson-1

Declaring Java Class Basic syntax of a Java Class – class { – –}

CSM-Java Programming-I Lesson-1 Declaring Java Class Example public class Book { private String bookName; //declare variable public Book(String Name) //declare constructor { bookName = Name; } public String getBookName() //declare method { return bookName; }

CSM-Java Programming-I Lesson-1 Object Creation Basic Syntax: new ClassName(parameters) Every object is created with a new operator. The object itself lives in memory somewhere in the JVM. Example: Book b = new Book(); –The variable b is not an object; it contains the address of the object (a reference to a Book object.)

CSM-Java Programming-I Lesson-1 Object Creation Object reference holds memory address of the object. If you use the following code to create an instance: Book newBook = new Book (name); then the following constructor must be defined in your class to create an instance. –public Book(String bname) {...}

CSM-Java Programming-I Lesson-1 Object Creation References are stored in variables, and variables have types that are specified by the programmer at compile time. For object references, assignment copies the memory location. E.g. obj1=obj2;

CSM-Java Programming-I Lesson-1 Declaring Constructors Basic syntax of a constructor: (modifier)ClassName(parameters) { ( statements…) } A constructor initializes an instance of a class. The name of the constructor must always be the same as the class name. Constructors do not have return values and are not inherited.

CSM-Java Programming-I Lesson-1 Declaring Constructors Example public class Customer { private String cusName; //declare variable public Customer(String Name) //declare constructor { cusName = Name; }

CSM-Java Programming-I Lesson-1 Default Constructors There is always at least one constructor in every class If the programmer does not supply any constructors, the default constructor will be present automatically –The default constructor takes no arguments –The default constructor takes no body

Difference Between Object And Class Set of objects with the same behavior is a Class. Book could be a class. Objects are instances of classes. If Book be a class then, each individual book is a unique instance of a class Book. The attributes and operations defined by a class are for its objects, not for itself. A class is a logical construct, an object has physical reality. CSM-Java Programming-I Lesson-1

Declaring Variables Basic Syntax of a variable ; can be one of the following values: byte | short | int | long | char | float | double | boolean | CSM-Java Programming-I Lesson-1

Defining Methods A class methods typically contain the code that manipulates an object’s state. All methods follow the same syntax: return-type method-name ( parameter-list ) { statement-list }

CSM-Java Programming-I Lesson-1 Defining Methods A method may contain local declarations as well as executable statements Variables declared locally can only be used locally The return type of a method indicates the type of value that the method sends back to the calling location A method that does not return a value (such as main ) has a void return type The return statement specifies the value that will be returned Its expression must conform to the return type

CSM-Java Programming-I Lesson-1 Defining Methods A method can be defined to accept zero or more parameters Each parameter in the parameter list is specified by its type and name The parameters in the method definition are called formal parameters The values passed to a method when it is invoked are called actual parameters

CSM-Java Programming-I Lesson-1 Object members Example public class Book { private String bookName; //declare variable public void setBookName (String Name) { bookName = Name; } public String getBookName() //declare method { return bookName; }

CSM-Java Programming-I Lesson-1 Accessing Object Members The “dot” operator is used to access members of the object. Examples b.setBookName(“Java ABC”); b.book_id = 123;

CSM-Java Programming-I Lesson-1 Garbage Collection When an object no longer has any valid references to it, it can no longer be accessed by the program, the space it occupies can be reclaimed. Java performs automatic garbage collection periodically. In other languages, the programmer has the responsibility for performing garbage collection

CSM-Java Programming-I Lesson-1 Pass by Value, Pass by Reference Java language only passes argument by value, i.e. Java passes a copy of value of the argument to the method. The term pass by reference means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. Java does not pass objects by reference; it passes object references by value.

CSM-Java Programming-I Lesson-1 The this keyword this is always a reference to the object on which the method was invoked. this can be used inside any method to refer to the current object. To pass the current object as a parameter to another method or constructor Example: –public class Book –{ –private String bookName; –public setBookName(String bookName) –{ –this.bookName = bookName; –}

CSM-Java Programming-I Lesson-1 Access Control Access control modifiers control access from other classes and inheritance by subclasses. Class members have four possible access control modifiers: Private: Members declared private are accessible only in the class itself. Package: Members declared with no access modifier are accessible in the class itself and are accessible to and inheritable by code in the same package.

CSM-Java Programming-I Lesson-1 Access Control Protected: Members declared protected are accessible in the class itself, and are accessible to and inheritable by code in the same package and code in subclasses. Public: Members declared with public modifier are accessible anywhere the class is accessible and inheritable by all subclasses.

CSM-Java Programming-I Lesson-1 OOP Principle - Encapsulation Encapsulation is the mechanism that binds together the code and the data it manipulates, and keeps both safe from outside interference and misuse. Hides the implementation details of a class behind operations performed on its internal data. Forces the user to use an interface to access data.

OOP Principle - Inheritance Inheritance is the process by which one object acquires the properties of another object. By use of inheritance, an object need only define all of its characteristics that make it unique within its class, it can inherit its general attributes from its parent. BankAccount CheckingSavingsCDs CSM-Java Programming-I Lesson-1

OOP Principle – Polymorphism Polymorphism(from Greek, meaning“many forms”) is a feature that allows one interface to be used for a general class of actions, i.e. one interface, multiple methods. Example: BankAccount.getInterest(); CSM-Java Programming-I Lesson-1

Java Code Conventions Packages – Package names should be nouns in lower case. Example: package shipping. objects Classes – Class names should be nouns, in mixed case, with the first letter of each word capitalized. Example: class Account

CSM-Java Programming-I Lesson-1 Java Code Conventions Methods – Method names should be verbs, in mixed case, with the first letter in lowercase. Within each method name, capital letters separate words. Limit the use of underscores. Example: getBalance() Variables – All variables should be in mixed case with a lowercase first letter. Words are separated by capital letters. Limit the use of underscores, and avoid using the dollar sign ($) because this character has special meaning to inner classes. Example: currentCustomer

CSM-Java Programming-I Lesson-1 Questions? Textbook Example Review Install Java Class Exercise: P2.8, P2.9, P2.12 Assignment: P2.5, P2.6, P2.11