1 / 82

Object-Oriented Programming in Java

Learn the fundamental concepts, principles, and techniques of object-oriented programming in Java. This course covers topics such as responsibility-driven design, interface, inheritance, encapsulation, iterators, overriding, coupling, cohesion, and more.

robertward
Download Presentation

Object-Oriented Programming in Java

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. Object-Oriented Programming in Java

  2. Buzzwords responsibility-driven design interface inheritance encapsulation iterators overriding coupling cohesion javadoc collection classes mutator methods polymorphic method calls CS2336: Object-Oriented Programming in Java

  3. What to Expect • Expect: fundamental concepts, principles, and techniques of object-oriented programming • Not to expect: language details, all Java APIs, JSP/JavaServlet, use of specific tools CS2336: Object-Oriented Programming in Java

  4. Fundamental concepts • object • class • method • parameter • data type CS2336: Object-Oriented Programming in Java

  5. What is OOP? • OOP: • identifying objects and having them collaborate with each other • procedural programming: • identifying steps that need to be taken to perform a task CS2336: Object-Oriented Programming in Java

  6. Objects and classes • objects • represent ‘things’ from the real world, or from some problem domain (example: “the red car down there in the car park”) • An object has a unique identity, state, and behaviors • classes • represent all objects of a kind (example: “car”) CS2336: Object-Oriented Programming in Java

  7. Methods and parameters • Objects have operations which can be invoked (Java calls them methods). • The behavior of an object is defined by a set of methods. • Methods may have parameters to pass additional information needed to execute. • Methods may return a result via a return value. • Method signature specifies the types of the parameters and return values CS2336: Object-Oriented Programming in Java

  8. Data Types • Parameters have data types, which specify what kind of data should be passed to a parameter. • There are two general kinds of types: primitive types, where values are stored in variables directly, and object types, where references to objects are stored in variables. CS2336: Object-Oriented Programming in Java

  9. Other observations • An object has attributes: values stored in data fields. • The state of an object consists of a set of datafields with their current values. CS2336: Object-Oriented Programming in Java

  10. Other observations • Many instances can be created from a single class. • The class defines what fields an object has, but each object stores its own set of values (the state of the object). • The class provides a special type of methods, known as constructors, which are invoked to construct objects from the class CS2336: Object-Oriented Programming in Java

  11. Classes CS2336: Object-Oriented Programming in Java

  12. Objects CS2336: Object-Oriented Programming in Java

  13. UML Class Diagram CS2336: Object-Oriented Programming in Java

  14. Constructors Constructors are a special kind of methods that are invoked to construct objects. Circle() { } Circle(double newRadius) { radius = newRadius; } CS2336: Object-Oriented Programming in Java

  15. Constructors, cont. • A constructor with no parameters is referred to as a no-arg constructor. • Constructors must have the same name as the class itself. • Constructors do not have a return type—not even void. • Constructors are invoked using the new operator when an object is created. • Constructors play the role of initializing objects. CS2336: Object-Oriented Programming in Java

  16. Creating Objects Using Constructors new ClassName(); Example: new Circle(); new Circle(5.0); CS2336: Object-Oriented Programming in Java

  17. Default Constructor • A class may be declared without constructors. • A no-arg constructor with an empty body is implicitly declared in the class. (a default constructor) • This constructor is provided automatically only if no constructors are explicitly declared in the class. CS2336: Object-Oriented Programming in Java

  18. Example:Ticket machines–an external view • Exploring the behavior of a typical ticket machine. • Use the naive-ticket-machine project. • Machines supply tickets of a fixed price. • Methods insertMoney, getBalance, and printTicket are used to enter money, keep track of balance, and print out tickets. CS2336: Object-Oriented Programming in Java

  19. Ticket machines – an internal view • Interacting with an object gives us clues about its behavior. • Looking inside allows us to determine how that behavior is provided or implemented. • All Java classes have a similar-looking internal view. CS2336: Object-Oriented Programming in Java

  20. Basic class structure The outer wrapper of TicketMachine publicclass TicketMachine { Inner part of the class omitted. } publicclassClassName { Fields Constructors Methods } The contents of a class CS2336: Object-Oriented Programming in Java

  21. Fields • Fields store values for an object. • They are also known as instance variables. • Fields define the state of an object. public class TicketMachine { private int price; private int balance; private int total; Further details omitted. } type visibility modifier variable name private int price; CS2336: Object-Oriented Programming in Java

  22. Constructors • Constructors initialize an object. • They have the same name as their class. • They store initial values into the fields. • They often receive external parameter values for this. public TicketMachine(int ticketCost) { price = ticketCost; balance = 0; total = 0; } CS2336: Object-Oriented Programming in Java

  23. Assignment • Values are stored into fields (and other variables) via assignment statements: • variable = expression; • price = ticketCost; • A variable stores a single value, so any previous value is lost. CS2336: Object-Oriented Programming in Java

  24. Accessor methods • Methods implement the behavior of objects. • Accessors provide information about an object. • Methods have a structure consisting of a header and a body. • The header defines the method’s signature. public int getPrice() • The body encloses the method’s statements. CS2336: Object-Oriented Programming in Java

  25. Accessor methods return type visibility modifier method name parameter list (empty) public int getPrice() { return price; } return statement start and end of method body (block) CS2336: Object-Oriented Programming in Java

  26. Test public class CokeMachine { private price; public CokeMachine() { price = 300 } public int getPrice { return Price; } • What is wrong here? (there are five errors!) CS2336: Object-Oriented Programming in Java

  27. Test public class CokeMachine { private price; public CokeMachine() { price = 300 } public int getPrice { return Price; } int • What is wrong here? ; (there are five errors!) () - } CS2336: Object-Oriented Programming in Java

  28. Mutator methods • Have a similar method structure: header and body. • Used to mutate (i.e., change) an object’s state. • Achieved through changing the value of one or more fields. • Typically contain assignment statements. • Typically receive parameters. CS2336: Object-Oriented Programming in Java

  29. Mutator methods return type visibility modifier method name parameter public void insertMoney(int amount) { balance = balance + amount; } field being mutated assignment statement CS2336: Object-Oriented Programming in Java

  30. Printing from methods public void printTicket() { // Simulate the printing of a ticket. System.out.println("##################"); System.out.println("# Ticket"); System.out.println("# " + price + " cents."); System.out.println("##################"); System.out.println(); // Update the total collected with the balance. total = total + balance; // Clear the balance. balance = 0; } CS2336: Object-Oriented Programming in Java

  31. Quiz-String concatenation • System.out.println(5 + 6 + "hello"); • System.out.println("hello" + 5 + 6); 11hello hello56 CS2336: Object-Oriented Programming in Java

  32. Local variables • Fields are one sort of variable. • They store values through the life of an object. • They are accessible throughout the class. • Methods can include shorter-lived variables. • They exist only as long as the method is being executed. • They are only accessible from within the method. CS2336: Object-Oriented Programming in Java

  33. Scope and life time • The scope of a local variable is the block it is declared in. • The lifetime of a local variable is the time of execution of the block it is declared in. CS2336: Object-Oriented Programming in Java

  34. Local variables A local variable public int refundBalance() { int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund; } CS2336: Object-Oriented Programming in Java

  35. Declaring Object Reference Variables To reference an object, assign the object to a reference variable. To declare a reference variable, use the syntax: ClassNameobjectRefVar; Example: Circle myCircle; CS2336: Object-Oriented Programming in Java

  36. Declaring/Creating Objectsin a Single Step ClassName objectRefVar = new ClassName(); Example: Circle myCircle = new Circle(); Create an object Assign object reference CS2336: Object-Oriented Programming in Java

  37. Accessing Objects • Referencing the object’s data: objectRefVar.data e.g., myCircle.radius • Invoking the object’s method: objectRefVar.methodName(arguments) e.g., myCircle.getArea() CS2336: Object-Oriented Programming in Java

  38. animation Trace Code Declare myCircle Circle myCircle = new Circle(5.0); SCircleyourCircle = new Circle(); yourCircle.radius = 100; myCircle no value CS2336: Object-Oriented Programming in Java

  39. animation Trace Code, cont. Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; myCircle no value Create a circle CS2336: Object-Oriented Programming in Java

  40. animation Trace Code, cont. Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; myCircle reference value Assign object reference to myCircle CS2336: Object-Oriented Programming in Java

  41. animation Trace Code, cont. Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; myCircle reference value yourCircle no value Declare yourCircle CS2336: Object-Oriented Programming in Java

  42. animation Trace Code, cont. Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; myCircle reference value yourCircle no value Create a new Circle object CS2336: Object-Oriented Programming in Java

  43. animation Trace Code, cont. Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; myCircle reference value yourCircle reference value Assign object reference to yourCircle CS2336: Object-Oriented Programming in Java

  44. animation Trace Code, cont. Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; myCircle reference value yourCircle reference value Change radius in yourCircle CS2336: Object-Oriented Programming in Java

  45. Caution- non-static methods • Circle.getArea() (WRONG) • objectRefVar.methodName(arguments) (e.g., myCircle.getArea()). • More explanations will be given in the section on “Static Variables, Constants, and Methods.” CS2336: Object-Oriented Programming in Java

  46. Reference Data Fields public class Student { String name; // name has default value null int age; // age has default value 0 boolean isScienceMajor; // isScienceMajor has default value false char gender; // c has default value '\u0000' } CS2336: Object-Oriented Programming in Java

  47. Example • Java assigns no default value to a local variable inside a method. public class Test { public static void main(String[] args) { int x; // x has no default value String y; // y has no default value System.out.println("x is " + x); System.out.println("y is " + y); } } Compilation error: variables not initialized CS2336: Object-Oriented Programming in Java

  48. Differences between Variables of Primitive Data Types and Object Types CS2336: Object-Oriented Programming in Java

  49. Copying Variables of Primitive Data Types and Object Types CS2336: Object-Oriented Programming in Java

  50. Garbage Collection The object previously referenced by c1 is no longer referenced. This object is known as garbage. Garbage is automatically collected by JVM. CS2336: Object-Oriented Programming in Java

More Related