Object-Oriented Programming in Java Chapter 1
Overview • Basics of objects and classes • Inheritance • Polymorphism • Abstract classes • Interfaces • Levels of access to class members • The root Object class • Generics
1.1 Objects and Encapsulation • Object-oriented language • Components that make up a Java application are considered to be objects that interact with each other.
1.1.1 Objects • Simulating a television on the computer. • Model the TV as an object. • A TV is activated when it is powered on. • The TV can be manipulated by changing its volume, the channel it is tuned to, its brightness and contrast, etc. • When it is powered off, the TV may retain its latest setting for the next time it is switched on.
1.1.1 Objects • Programs may model non-physical objects as well. • A graphical drawing package. • Keeps track of all the figures the user draws on the drawing pad, so it knows when, where, and how to display these figures on the pad. • Example: Rectangles. • An (x, y) location on the plane (i.e. The bottom-left corner of the rectangle) • A width, w • A height, h.
1.1.2 Lifetime, State and Messages • Creation of a TV object. • TV objects will exist as long as the simulation runs. • The state of the TV may be changed by sending it messages to switch channels, change volume, etc. • When a message is sent to the TV, it knows what to do. • State of a TV object would comprise a set of attributes, a variable with a certain value. • Values of all the attributes together determine a current state—if any one of these values is changed, the state is changed.
1.1.2 Lifetime, State and Messages • The data and functions are therefore encapsulated or enclosed within a single entity that is the TV object. • The data are called fields, and the operations, methods. • A Rectangle • Fields • x and y coordinates, and its width and height values, w and h, • Methods • drawing the Rectangle on the display, moving it, resizing it, and so forth.
1.1.3 Clients of an Object • The sender of messages to an object is usually referred to as the client of that object. • An object may have several clients that send messages to it at different points in the lifetime of the object.
1.1.4 Separation of Interface from Implementation • The separation of interface from implementation. • What an object is capable of doing is its behavior, which is available to its clients via its methods, or interface. • Exactly how this behavior is implemented by the object is not known to its clients.
1.2 Classes • A class serves as a blueprint from which any number of objects may be crated.
1.2.1 State and Behavior • The state TV is defined by its fields channel and volume. • A TV can switchChannel, changeVolume, and recallChannel. • switchChannel and changeVolume change the state of the TV, recallChannel simply accesses the state. • MAX.VOLUME • Rectangle fields x, y, w, and h define the state of any Rectangle. • Behavior defined by the methods moveTo, resize, getX, getY, getWidth, and getHeight.
1.2.2 Method Overloading • Signature of a method name, and the number, sequence, and type of its parameters. • The return type is not part of the signature. • The following have the same signature:
1.2.2 Method Overloading • In a class, no two methods can have the same signature—if there are it does not compile the program.
1.2.2 Method Overloading • The signatures of those methods are different, since they have different types of arguments. • Contains method is overloaded.
1.2.3 Object Creation, Constructors, Garbage Collection • The left-hand declares myTV to be a reference of type TV. • The right-hand side creates a TV object. • All objects must be created with new. • aTV does not refer to anything.
1.2.3 Object Creation, Constructors, Garbage Collection • Creation occurs in two phases: • Allocating memory space for it, and initializes all its fields to their default values. • The no-arg constructor (re)initializes its state as required. • A constructor: • Has the same name as its class; • Has no return value.
1.2.3 Object Creation, Constructors, Garbage Collection • No-arg Constructor • A constructor that takes no parameters,. • The TV class has a no-arg constructor, which was used to initialize a new TV object earlier. • The Rectangle class does not have a no-arg constructor.
1.2.3 Object Creation, Constructors, Garbage Collection • Parametered constructors • If TV objects need to be created with other initial values for channel or volume, the parametered constructor may be used instead:
1.2.3 Object Creation, Constructors, Garbage Collection • Constructor code reuse • Calls the parametered constructor of the TV class. • Could have been: • Using this(...) reuses the code.
1.2.3 Object Creation, Constructors, Garbage Collection • Explicit reference to current object. • This is a reference to the object on which the constructor is invoke. • This is essential, the parameters channel and volume would be indistinguishable from the fields of the same names. • The parameters, hide the class fields of the same name.
1.2.3 Object Creation, Constructors, Garbage Collection • Compiler-supplied default constructor • Default constructor, no-arg construction with an empty body. • Initializing the fields in the object to default initial values based on their types. • Even when we create an object in class TV, which does define constructors (i.e. There will not be a default constructor supplied by the compiler).
1.2.3 Object Creation, Constructors, Garbage Collection • Garbage collection • Memory space occupied by an object is reclaimed by the garbage collector when the object is no longer in use. • The first Rectangle object (10, 10) has no variable referring to it. • Not accessible any more, and is considered to be no longer in use.
1.2.3 Object Creation, Constructors, Garbage Collection • Garbage collector • Identifies that an object is not in use. • Determines the best time to reclaim the space used by this object. • Garbage collector is always silently running in the background. • Memory that is allocated when a new object is created, is automatically garbage collected when it is no longer in use.
1.2.3 Object Creation, Constructors, Garbage Collection • C and C++ • Two memory-related problems: • The programmer “forgets” to free up space that had been allocated earlier, but is no longer required (wasteful). • The programmer tries to use memory that has been freed up earlier, without realizing that it no longer belongs to the program (dangerous).
1.2.5 Static Fields and Methods • Static fields • Switching the channel on tv1 has not affected the channel setting on tv2 because their respective channel fields are separate and different.
1.2.5 Static Fields and Methods • Static variables • Objects of a class may share a common resource. • This is a property of the class as a whole, not of every object. • MAX_VOLUME set a limit on the value of the volume field. • All TV objects will be able to share the MAX_VOLUME resource. • It may be accessed by using the class name instead of an object name:
1.2.5 Static Fields and Methods • Access via the class name is recommended because it is then obvious that the field is static. • Constants • final indicates that once a value is assigned to this field, it may not subsequently change.
1.2.5 Static Fields and Methods • Static Methods • Utility methods applies to the class rather than any particular object of that class. • Invoked through the name of the class that contains it and not through an object. • All the methods the class Math are static, and so are its fields.
1.2.5 Static Fields and Methods • Static methods can control the number of instances of a class that may be created in an application. • DrawingManager keeps track of all figures in a drawing application, and does various other managerial activities. • We only need one DrawingManager object in the application.
1.2.5 Static Fields and Methods • The compiler will call an error because the constructor is not visible outside the class. • Define a method that can deal out the one instance of DrawingManager. • Must be static because it cannot be called on a DrawingManager object.
1.2.5 Static Fields and Methods • All accesses are forced to be made through the getInstance method, which correctly handles the null case.
1.2.5 Static Fields and Method • Special static method main • main method is declared as static.
1.2.6 Object References • When an object is created using the new construct, it is assigned space in memory. • Two distinct objects • One object that is reference by two names
1.2.6 Object References • Consider a method that takes some object as parameter is called. • When objects are passed as a parameter to a method, the parameter is a reference to the original object and not a copy. • Any changes to the object in the method will be reflected in the object outside the method.
1.3 Inheritance • Notion of specialization that leads to reuse of code is the basis of inheritance in object-oriented programming.
1.3.1 Superclass and Subclass • B is a specialization of class A. • A is inherited (and therefore reused) by class B. • B is subclass. • A is superclass.
1.3.1 Superclass and Subclass • Cars and motorcycles are both motorized vehicles. • A car has four wheels instead of two, a steering wheel instead of a steering bar, and so on. • Inheritance necessitates an is-A relationship between the class that inherits and the class that is inherited from. • Every Car is a MotorVehicle.
1.3.1 Superclass and Subclass • There are two methods in StereoTV, namely checkSetBalance and setLeftRight, that simply serve as helpers to the other methods of the class (they are not declared public).