DOC PREVIEW
Penn CIT 591 - Basic Object Oriented concepts

This preview shows page 1-2-17-18-19-36-37 out of 37 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 their 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: You can still use overridden methodsConcept: Constructors make objectsSyntax for constructorsTrick: Use the same name for a parameter as for a fieldInternal workings: Constructor chainingThe case of the vanishing constructorExample: Broken constructor chainFixing a broken constructor chainTrick: one constructor calling anotherConcept: You can control accessConcept: Classes themselves can have fields and methodsExample of a class variableAdvice: Restrict accessAdvice: Use setters and gettersKinds of accessThe EndBasic Object-Oriented conceptsConcept: 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 objectsConcept: An object has state•An object contains both data and methods that manipulate that data–The data represent the state of the object–Data can also describe the relationships between this object and other objects•Example: A CheckingAccount might have–A balance (the internal state of the account)–An owner (some object representing a person)Example: A “Rabbit” object•You could (in a game, for example) create an object representing a rabbit•It would have data:–How hungry it is–How frightened it is–Where it is•And methods:–eat, hide, run, digConcept: 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 cutterConcept: 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•Example: a CheckingAccount, with operations deposit, withdraw, getBalance, etc.•Classes enforce this bundling togetherExample of a classclass Employee { // fields String name; double salary; // a method void pay () { System.out.println("Pay to the order of " + name + " $" + salary); }}Approximate Terminology•instance = object•field = variable•method = function•sending a message to an object = calling a function•These are all approximately trueConcept: Classes form a hierarchy•Classes are arranged in a treelike structure called a hierarchy•The class at the root is named Object•Every class, except Object, has a superclass•A class may have several ancestors, up to Object•When you define a class, you specify its superclass–If you don’t specify a superclass, Object is assumed•Every class may have one or more subclassesExample of (part of) a hierarchyA FileDialog is a Dialog is a Window is a Container.ContainerPanel ScrollPaneWindowDialogFrameFileDialogC++ 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 hierarchyConcept: Objects inherit from their 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!Example of inheritanceclass Person { String name; String age; void birthday () { age = age + 1; }}class Employee extends Person { double salary; void pay () { ...}}Every Employee has a name, age, and birthday method as well as a salary and a pay method.Concept: Objects must be created•int n; does two things:–it declares that n is an integer variable–it allocates space to hold a value for n•Employee secretary; does one thing–it declares that secretary is type Employee•secretary = new Employee ( ); allocates the spaceNotation: How to declare and create objects Employee secretary; // declares secretary secretary = new Employee (); // allocates space Employee secretary = new Employee(); // both•But the secretary is still "blank" secretary.name = "Adele"; // dot notation secretary.birthday (); // sends a messageNotation: 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!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 thisConcept: A variable can hold subclass objects•Suppose B is a subclass of A–A objects can be assigned to A variables–B objects can be assigned to B variables–B objects can be assigned to A variables, but–A objects can not be assigned to B variables•Every B is also an A but not every A is a B•You can cast: bVariable = (B) aObject;–In this case, Java does a runtime checkExample: Assignment of subclassesclass Dog { ... }class Poodle extends Dog { ... }Dog myDog;Dog rover = new Dog ();Poodle yourPoodle;Poodle fifi = new Poodle (); myDog = rover; // okyourPoodle = fifi; // okmyDog = fifi; //okyourPoodle = rover; // illegalyourPoodle = (Poodle) rover; //runtime checkConcept: Methods can be overridden•So birds can fly. Except penguins.class Bird extends Animal { void fly (String destination) { location = destination;


View Full Document

Penn CIT 591 - Basic Object Oriented concepts

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Download Basic Object Oriented concepts
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 Basic Object Oriented concepts 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 Basic Object Oriented concepts 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?