Download Object-Oriented Programming Concepts: Classes, Objects, and Access Modifiers and more Exercises Java Programming in PDF only on Docsity! at Basic Object-Oriented Concepts
py -
3-Jul-12
docsity.com
2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions, which could manipulate any data An object contains both data and methods that manipulate that data An object is active, not passive; it does things An object is responsible for its own data But: it can expose that data to other objects docsity.com 5 Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields, or variables The class describes those fields An object may have methods The class describes those methods A class is like a template, or cookie cutter You use the class’s constructor to make objects docsity.com 6 Concept: Classes are like Abstract Data Types An Abstract Data Type (ADT) bundles together: some data, representing an object or "thing" the operations on that data The operations defined by the ADT are the only operations permitted on its data Example: a CheckingAccount, with operations deposit, withdraw, getBalance, etc. Classes enforce this bundling together If all data values are private, a class can also enforce the rule that its defined operations are the only ones permitted on the data docsity.com 7 Example of a class class 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 } docsity.com 10 Example of (part of) a hierarchy A FileDialog is a Dialog is a Window is a Container Container Panel ScrollPane Window Dialog Frame FileDialog docsity.com 11 C++ is different In C++ there may be more than one root but not in Java! In C++ an object may have more than one parent (immediate superclass) but not in Java! Java has a single, strict hierarchy docsity.com 12 Concept: Objects inherit from superclasses A class describes fields and methods Objects of that class have those fields and methods But an object also inherits: the fields described in the class's superclasses the methods described in the class's superclasses A class is not a complete description of its objects! docsity.com 15 Notation: How to declare and create objects Employee secretary; // declares secretary secretary = new Employee (); // allocates space Employee secretary = new Employee(); // does both But the secretary is still "blank" (null) secretary.name = "Adele"; // dot notation secretary.birthday (); // sends a message docsity.com 16 Notation: How to reference a field or method Inside 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! docsity.com 17 Concept: this object Inside a class, no dots are necessary, because you are working on this object If you wish, you can make it explicit: class Person { ... this.age = this.age + 1; ...} this is like an extra parameter to the method You usually don't need to use this docsity.com 20 Concept: Methods can be overridden So birds can fly. Except penguins. class Bird extends Animal { void fly (String destination) { location = destination; } } class Penguin extends Bird { void fly (String whatever) { } } docsity.com 21 Concept: Don't call functions, send messages Bird someBird = pingu; someBird.fly ("South America"); Did pingu actually go anywhere? You sent the message fly(...) to pingu If pingu is a penguin, he ignored it Otherwise he used the method defined in Bird You did not directly call any method You cannot tell, without studying the program, which method actually gets used The same statement may result in different methods being used at different times docsity.com 22 Sneaky trick: How to use overridden methods class FamilyMember extends Person { void birthday () { // override birthday() in Person super.birthday (); // call overridden method givePresent (); // and add your new stuff } } docsity.com 25 Trick: Give field and parameter the same name A parameter overrides a field with the same name But you can use this.name to refer to the field class Person { String name; int age; Person (String name, int age) { this.name = name; this.age = age; } } Using the same name is a common and useful convention docsity.com 26 Internal workings: Constructor chaining If an Employee is a Person, and a Person is an Object, then when you say new Employee () The Employee constructor calls the Person constructor The Person constructor calls the Object constructor The Object constructor creates a new Object The Person constructor adds its own stuff to the Object The Employee constructor adds its own stuff to the Person docsity.com 27 The case of the vanishing constructor If you don't write a constructor for a class, Java provides one (the default constructor) The one Java provides has no arguments If you write any constructor for a class, Java does not provide a default constructor Adding a perfectly good constructor can break a constructor chain You may need to fix the chain docsity.com 30 Trick: one constructor calling another this(...) calls another constructor for this same class It is poor style to have the same code more than once If you call this(...), that call must be the first thing in your constructor class Something { Something (int x, int y, int z) { // do a lot of work here } Something ( ) { this (0, 0, 0); } } docsity.com 31 Concept: You can control access class Person { public String name; private String age; protected double salary; public void birthday { age++; } } Each object is responsible for its own data Access control lets an object protect its data and its methods Access control is the subject of a different lecture docsity.com 32 Concept: Classes can have fields and methods Usually a class describes fields (variables) and methods for its objects (instances) These are called instance variables and instance methods A class can have its own fields and methods These are called class variables and class methods There is exactly one copy of a class variable, not one per object Use the special keyword static to say that a field or method belongs to the class instead of to objects docsity.com 35 Advice: Use setters and getters This way the object maintains control Setters and getters have conventional names: setDataName, getDataName, isDataName (booleans only) class Employee extends Person { private double salary; private boolean male; public void setSalary (double newSalary) { salary = newSalary; } public double getSalary () { return salary; } public boolean isMale() { return male; } } docsity.com 36 Kinds of access Java provides four levels of access: public: available everywhere protected: available within the package (in the same subdirectory) and to all subclasses [default]: available within the package private: only available within the class itself The default is called package visibility In small programs this isn't important...right? docsity.com oft The End
37
docsity.com