Unformatted text preview:

What is an object?What is an object?Why use objects?ClassesAnatomy of a classConstructorsUsing ObjectsThe LightSwitch ClassA Different LightSwitch ClassAbstractionWhy is Abstraction Important?Breaking the Abstraction BarrierPublic versus PrivateA Better LightSwitchEnforcing the Abstraction BarrierPrimitives vs ObjectsReferencesReference InequalityReference EqualityEquality Quiz 1Equality Quiz 2Java APILecture 8 Classes and Objects Part 2MIT AITI June 15th, 2005©2005MIT-Africa Internet Technology InitiativeWhat is an object?• A building (Strathmore university)• A desk• A laptop• A car• Data packets through the internet©2005MIT-Africa Internet Technology InitiativeWhat is an object?• Objects have two parts:z State: Properties of an object.z Behavior: Things the object can do.• Car Example:z State: Color, engine size, automaticz Behavior: Brake, accelerate, shift gear• Person Example:z State: Height, weight, gender, agez Behavior: Eat, sleep, exercise, study©2005MIT-Africa Internet Technology InitiativeWhy use objects?• Modularity: Once we define an object, we can reuse it for other applications.• Information Hiding: Programmers don’t need to know exactly how the object works. Just the interface.• Example:z Different cars can use the same parts. z You don’t need to know how an engine works in order to drive a car.©2005MIT-Africa Internet Technology InitiativeClasses• A class is a template or pattern from which objects are created• A class containsz Data members (Properties/Characteristics of the objects/class)z Methods (Determines the behavior of the objects created from the class)z Constructor (Special Method)©2005MIT-Africa Internet Technology InitiativeAnatomy of a class• You have all seen classes in your labs• Basic anatomyz public class className{z Data membersz Constructorz Methods}©2005MIT-Africa Internet Technology InitiativeConstructors• Constructors provide objects with the data they need to initialize themselves, like “How to Assemble” instructions. • Objects have a default constructor that takes no arguments, like LightSwitch().• We can define our own constructors that take any number of arguments.• Constructors have NO return type and must be named the same as the class:z ClassName(argument signature) { body }©2005MIT-Africa Internet Technology InitiativeRecall the LightSwitch Class• class LightSwitch { boolean on = true; }• The keyword class tells java that we’re defining a new type of Object.• Classes are a blueprint.• Objects are instances of classes.• Everything in Java (except primitives) are Objects and have a Class.©2005MIT-Africa Internet Technology InitiativeUsing Objectspublic static void main(String[] args) {LightSwitch s = new LightSwitch();System.out.println(s.isOn);s.flip();System.out.println(s.isOn);}• The new keyword creates a new object.• new must be followed by a constructor.• We call methods like:z variableName.methodName(arguments)©2005MIT-Africa Internet Technology InitiativeThe LightSwitch Classclass LightSwitch {boolean on = true;boolean isOn() {return on;}void switch() {on = !on;}}©2005MIT-Africa Internet Technology InitiativeA Different LightSwitch Classclass LightSwitch {int on = 1;boolean isOn() {return on == 1;}void switch() {on = 1 - on;}}©2005MIT-Africa Internet Technology InitiativeAbstraction• Both LightSwitch classes behave the same.• We treat LightSwitch as an abstraction: we do not care about the internal code of LightSwitch, only the external behavior• Internal code = implementation• External behavior = interface©2005MIT-Africa Internet Technology InitiativeWhy is Abstraction Important?• We can continue to refine and improve the implementation of a class so long as the interface remains the same.• All we need is the interface to an Object in order to use it, we do not need to know anything about how it performs its prescribed behavior.• In large projects involving several teams, programmers only need to know what is necessary for their part of the code (eg. Microsoft, Google, Goldman Sachs, Morgan Stanley and other financial companies)©2005MIT-Africa Internet Technology InitiativeBreaking the Abstraction Barrier• A user of LightSwitch that relied on the boolean field would break if we changed to an integer fieldclass AbstractionBreaker {public static void main(String[] args) {LightSwitch ls = new LightSwitch();if (ls.on) // now broken!System.out.println("light is on");elseSystem.out.println("light is off");}}©2005MIT-Africa Internet Technology InitiativePublic versus Private• Label fields and methods private to ensure other classes can't access them• Label fields and methods public to ensure other classes can access them.• If they are not labeled public or private, for now consider them public.©2005MIT-Africa Internet Technology InitiativeA Better LightSwitchclass LightSwitch {private boolean on = true;public boolean isOn() {return on;}public void switch() {on = !on;}}©2005MIT-Africa Internet Technology InitiativeEnforcing the Abstraction Barrier• By labeling the on field private . . .• Now AbstractionBreaker's attempt to access the on field would not have compiled to begin with.class LightSwitch {private boolean on = true;// . . .}if (ls.on) // would never have compiled©2005MIT-Africa Internet Technology InitiativePrimitives vs Objects• Two datatypes in Java: primitives and objects• Primitives: byte, short, int, long, double, float, boolean, char== tests if two primitives have the same value• Objects: defined in Java classes== tests if two objects are the same object©2005MIT-Africa Internet Technology InitiativeReferences• The new keyword always constructs a new unique instance of a class• When an instance is assigned to a variable, that variable is said to hold a reference or point to that object• g and h hold references to two different objects that happen to have identical statePerson g = new Person(“Mwangi", 21);Person h = new Person(“Mwangi", 21);©2005MIT-Africa Internet Technology InitiativeReference Inequality• g != h because g and h hold references to different objectsPerson g = new Person(“Mwangi", 21);Person h = new Person(“Mwangi", 21);gh“Mwangi"21“Mwangi"21©2005MIT-Africa Internet Technology InitiativeReference Equality• greg1 == greg2 because greg1 and greg2 hold references to the same objectPerson greg1 = new Person("Greg", 23);Person greg2 =


View Full Document

MIT SP 772 - Classes and Objects

Download Classes and Objects
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Classes and Objects and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Classes and Objects 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?