Basic Object-Oriented ConceptsConcept: An object has behaviorsConcept: An object has stateExample: A “Rabbit” objectConcept: Classes describe objectsConcept: Classes are like Abstract Data TypesExample of a classApproximate TerminologyConcept: Classes form a hierarchyExample of (part of) a hierarchyC++ is differentConcept: Objects inherit from superclassesExample of inheritanceConcept: Objects must be createdNotation: How to declare and create objectsNotation: How to reference a field or methodConcept: this objectConcept: A variable can hold subclass objectsExample: Assignment of subclassesConcept: Methods can be overriddenConcept: Don't call functions, send messagesSneaky trick: How to use overridden methodsConcept: Constructors make objectsSyntax for constructorsTrick: Give field and parameter the same nameInternal workings: Constructor chainingThe case of the vanishing constructorExample: Broken constructor chainFixing a broken constructor chainTrick: one constructor calling anotherConcept: You can control accessConcept: Classes can have fields and methodsExample of a class variableAdvice: Restrict accessAdvice: Use setters and gettersKinds of accessThe EndJan 14, 2019Basic Object-Oriented Concepts2Concept: An object has behaviorsIn old style programming, you had:data, which was completely passivefunctions, which could manipulate any dataAn object contains both data and methods that manipulate that dataAn object is active, not passive; it does thingsAn object is responsible for its own dataBut: it can expose that data to other objects3Concept: An object has stateAn object contains both data and methods that manipulate that dataThe data represent the state of the objectData can also describe the relationships between this object and other objectsExample: A CheckingAccount might haveA balance (the internal state of the account)An owner (some object representing a person)4Example: A “Rabbit” objectYou could (in a game, for example) create an object representing a rabbitIt would have data:How hungry it isHow frightened it isWhere it isAnd methods:eat, hide, run, dig5Concept: Classes describe objectsEvery object belongs to (is an instance of) a classAn object may have fields, or variablesThe class describes those fieldsAn object may have methodsThe class describes those methodsA class is like a template, or cookie cutterYou use the class’s constructor to make objects6Concept: Classes are like Abstract Data TypesAn Abstract Data Type (ADT) bundles together:some data, representing an object or "thing"the operations on that dataThe operations defined by the ADT are the only operations permitted on its dataExample: a CheckingAccount, with operations deposit, withdraw, getBalance, etc.Classes enforce this bundling togetherIf all data values are private, a class can also enforce the rule that its defined operations are the only ones permitted on the data7Example of a classclass Employee { // Fields private String name; //Can get but not change private double salary; // Cannot get or set // Constructor Employee(String n, double s) { name = n; salary = s; } // Methods void pay () { System.out.println("Pay to the order of " + name + " $" + salary); } public String getName() { return name; } // getter}8Approximate Terminologyinstance = objectfield = instance variablemethod = functionsending a message to an object = calling a functionThese are all approximately true9Concept: Classes form a hierarchyClasses are arranged in a treelike structure called a hierarchyThe class at the root is named ObjectEvery class, except Object, has a superclassA class may have several ancestors, up to ObjectWhen you define a class, you specify its superclassIf you don’t specify a superclass, Object is assumedEvery class may have one or more subclasses10Example of (part of) a hierarchyA FileDialog is a Dialog is a Window is a ContainerContainerPanel ScrollPaneWindowDialogFrameFileDialog11C++ is differentIn C++ there may be more than one rootbut not in Java!In C++ an object may have more than one parent (immediate superclass)but not in Java!Java has a single, strict hierarchy12Concept: Objects inherit from superclassesA class describes fields and methodsObjects of that class have those fields and methodsBut an object also inherits:the fields described in the class's superclassesthe methods described in the class's superclassesA class is not a complete description of its objects!13Example of inheritanceclass Person { String name; int age; void birthday () { age = age + 1; }}class Employee extends Person { double salary; void pay () { ...}}Every Employee has name and age fields and birthday method as well as a salary field and a pay method.14Concept: Objects must be createdint n; does two things:It declares that n is an integer variableIt allocates space to hold a value for nFor a primitive, this is all that is neededEmployee secretary; also does two thingsIt declares that secretary is type EmployeeIt allocates space to hold a reference to an EmployeeFor an object, this is not all that is neededsecretary = new Employee ( );This allocate space to hold a value for the EmployeeUntil you do this, the Employee is null15Notation: How to declare and create objects Employee secretary; // declares secretary secretary = new Employee (); // allocates space Employee secretary = new Employee(); // does bothBut the secretary is still "blank" (null) secretary.name = "Adele"; // dot notation secretary.birthday (); // sends a message16Notation: How to reference a field or methodInside a class, no dots are necessary class Person { ... age = age + 1; ...}Outside a class, you need to say which object you are talking to if (john.age < 75) john.birthday ();If you don't have an object, you cannot use its fields or methods!17Concept: this objectInside a class, no dots are necessary, becauseyou are working on this objectIf you wish, you can make it explicit: class Person { ... this.age = this.age + 1; ...}this is like an extra parameter to the methodYou usually don't need to use this18Concept: A variable can hold subclass objectsSuppose B is a subclass of AA objects can be assigned to A variablesB objects can be
View Full Document