820 likes | 852 Views
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.
E N D
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
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
Fundamental concepts • object • class • method • parameter • data type CS2336: Object-Oriented Programming in Java
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
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
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
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
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
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
Classes CS2336: Object-Oriented Programming in Java
Objects CS2336: Object-Oriented Programming in Java
UML Class Diagram CS2336: Object-Oriented Programming in Java
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
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
Creating Objects Using Constructors new ClassName(); Example: new Circle(); new Circle(5.0); CS2336: Object-Oriented Programming in Java
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Quiz-String concatenation • System.out.println(5 + 6 + "hello"); • System.out.println("hello" + 5 + 6); 11hello hello56 CS2336: Object-Oriented Programming in Java
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
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
Local variables A local variable public int refundBalance() { int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund; } CS2336: Object-Oriented Programming in Java
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
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
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
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
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
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
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
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
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
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
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
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
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
Differences between Variables of Primitive Data Types and Object Types CS2336: Object-Oriented Programming in Java
Copying Variables of Primitive Data Types and Object Types CS2336: Object-Oriented Programming in Java
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