1 / 32

CSM-Java Programming-I Spring,2005

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

fineen
Télécharger la présentation

CSM-Java Programming-I Spring,2005

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. 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. CSM-Java Programming-I Lesson-1

  4. 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

  5. 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

  6. 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

  7. Declaring Java Class Basic syntax of a Java Class • < modifier> class < classname> { • < attribute_declaration> • < constructor_declaration> • < method_declaration> • } CSM-Java Programming-I Lesson-1

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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 CSM-Java Programming-I Lesson-1

  15. 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

  16. Declaring Variables Basic Syntax of a variable <modifier> <type> <variable_name>; <type> can be one of the following values: byte | short | int | long | char | float | double | boolean | <class_name> CSM-Java Programming-I Lesson-1

  17. Defining Methods • A class methods typically contain the code that manipulates an object’s state. • All methods follow the same syntax: return-typemethod-name ( parameter-list ) { statement-list } CSM-Java Programming-I Lesson-1

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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. CSM-Java Programming-I Lesson-1

  28. 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 Checking Savings CDs CSM-Java Programming-I Lesson-1

  29. 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

  30. 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

  31. 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

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

More Related