Introduction to OOP

Classes, Objects, Methods and Instance Variables

Developing solution to computational problem by using and/or composing objects.Object Oriented Programming  What is object ?   An entity designed specifically to do some specific tasks. Usually denotes by noun or noun phrase.  What is OOP ?  .

Reusable components. Adaptable to many situation.OOP Paradigms  Encapsulation :  Hides the complexity from the user.  Inheritance :   Polymorphism  .

Every object maintains its own attributes. Determine capability of an object. Also used to modify the attributes of an object.OOP Paradigms.  Interfaces   . cont…  Properties/Attributes:    Determine behavior of an object. “Private”.

and interfaces on a ATM ! .Problem  Define objects. attributes.

 Objects   .OOP in Java  Class     Blueprints of an object. Name of class must same with the name of file Special Class: Class that has method “main” is automatically realized Realization of the class. Contains methods and fields of Instance Variables. Reference variables.

OOP in Java. cont…  Methods    Interface of an object. Should be “public” Constructor:    Special method for initializing class. Have name same as class. Special methods for modify attributes.  set and get Methods  . Called automatically when a class is realize/instantiate.

Automatically initialize. . should be modified through set and get methods only. Sometimes referred as fields of instance variables (or fields for short). Usually placed before any methods Strongly recommended to have “private” access modifier. cont…  Instance Variables      Attributes of a class.OOP in Java.

17. 23. } // end method displayMessage } // end class GradeBook . 15. 20. 12. 14. 22. 10. // course name for this GradeBook // constructor initializes courseName with String supplied as argument public GradeBook( String name ) { courseName = name. // initializes courseName } // end constructor // method to set the course name public void setCourseName( String name ) { courseName = name. 5. 18. 4.out.Class Example 1. 25. 7. getCourseName() ). 9. 19. } // end method getCourseName // display a welcome message to the GradeBook user public void displayMessage() { // this statement calls getCourseName to get the name of the course this GradeBook represents System. 13. 24. 21. 3. 11. 8. 6. 16.printf( "Welcome to the grade book for\n%s!\n". // store the course name } // end method setCourseName // method to retrieve the course name public String getCourseName() { return courseName. public class GradeBook { private String courseName. 2.

out. GradeBook gradeBook2 = new GradeBook("CS102 Data Structures in Java" ). 12. 7.printf( "gradeBook1 course name is: %s\n". 9.out. 14. } // end main } // end class GradeBookTest 13. // display initial value of courseName for each GradeBook System. 6. 10. 3. 11. 8.getCourseName() ).printf( "gradeBook2 course name is: %s\n". gradeBook1. System. 4.getCourseName() ). 5. gradeBook2. 2. public class GradeBookTest { // main method begins program execution public static void main( String args[] ) { // create GradeBook object GradeBook gradeBook1 = new GradeBook("CS101 Introduction to Java Programming" ). .Realization of a Class 1.

Problem  Define the ATM Objects in Java .

A compilation error occurs if the types of the arguments in a method call are not consistent with the types of the corresponding parameters in the method declaration. Normally. such as "hello". A compilation error occurs if the number of arguments in a method call does not match the number of parameters in the method declaration. . But most Java programmers consider using fully qualified names to be cumbersome. objects are created with new. String literals are references to String objects that are implicitly created by Java. One exception is a string literal that is contained in quotes.Notes      Declaring more than one public class in the same file is a compilation error. and instead prefer to use import declarations. The Java compiler does not require import declarations in a Java source code file if the fully qualified class name is specified every time a class name is used in the source code.

) We prefer to list the fields of a class first. if they will be accessed only by other methods of the class. so that. but scattering them tends to lead to hard-to-read code.Notes. instance variables should be declared private and methods should be declared public. It is possible to list the class’s fields anywhere in the class outside its method declarations. Place a blank line between method declarations to separate the methods and enhance program readability. (We will see that it is appropriate to declare certain methods private. as you read the code. cont…     Precede every field and method declaration with an access modifier. As a rule of thumb. Any attempt to use a local variable that has not been initialized results in a compilation error. . you see the names and types of the variables before you see them used in the methods of the class.

.Notes. Unless default initialization of your class’s instance variables is acceptable. int. Using floating-point numbers in a manner that assumes they are represented precisely can lead to logic errors. Account account1 indicates that account1 is a reference to an Account object).. For example.g. provide a constructor to ensure that your class’s instance variables are properly initialized with meaningful values when each new object of your class is created. If a variable’s type is not one of the eight primitive types. double or GradeBook) indicates whether the variable is of a primitive or a reference type. then it is a reference type. cont…    A variable’s declared type (e.