DOC PREVIEW
Penn CIT 591 - Everything You Ever Wanted To Know About Java

This preview shows page 1-2-16-17-18-33-34 out of 34 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 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 34 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 34 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 34 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 34 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 34 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 34 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Everything You Ever Wanted To Know About Java O-OWhat is a class?ClassesWhy inheritance?What are abstract classes for?InterfacesDeclarations and assignmentsWhat are interfaces for?Inner Classes IInner Classes IIWhat are inner classes for?Enumerationsenums are classesEnums really are classesOther features of enumsUsing generic classesDefining generic classesAccessProper use of fieldsComposition and inheritanceConstructorsConstructor chainingProper use of constructorsReferencesMethods IMethods IIMethods IIIGeneric methods with wildcardsvarargsProper use of methods IProper use of methods IIProper use of polymorphismProgram designThe EndJan 14, 2019Everything You Ever Wanted To Know About Java O-O2What is a class?A class is primarily a description of objects, or instances, of that classA class contains one or more constructors to create objectsA class is a typeA type defines a set of possible values, and operations on those valuesThe type of an object is the class that created itBut a class can also contain information about itselfAnything declared static belongs to the class itselfStatic variables contain information about the class, not about instances of the classStatic methods are executed by the class, not by instances of the classAnything not declared static is not part of the class, and cannot be used directly by the classHowever, a static method can create (or be given) objects, and can send messages to them3Classesclass MyClass extends ThatClass implements SomeInterface, SomeOtherInterface {...}A top-level class can be public or package (default)A class can be final, meaning it cannot be subclassedA class subclasses exactly one other class (default: Object)A class can implement any number of interfacesabstract class MyClass extends ThatClass implements SomeInterface, SomeOtherInterface {...}Same rules as above, except: An abstract class cannot be finalA class must be declared abstract if:It contains abstract methodsIt implements an interface but does not define all the methods of that interfaceAny class may be declared to be abstractAn abstract class can (and does) have constructorsYou cannot instantiate an abstract class4Why inheritance?Java provides a huge library of pre-written classesSometimes these classes are exactly what you needSometimes these classes are almost what you needIt’s easy to subclass a class and override the methods that you want to behave differentlyInheritance is a way of providing similar behavior to different kinds of objects, without duplicating codeYou should extend a class (and inherit from it) only if:Your new class really is a more specific kind of the superclass, a ndYou want your new class to have most or all of the functionality of the class you are extending, andYou need to add to or modify the capabilities of the superclassYou should not extend a class merely to use some of its featuresComposition is a better solution in this case5What are abstract classes for?Abstract classes are suitable when you can reasonably implement some, but not all, of the behavior of the subclassesExample: You have a game in which various kinds of animals move around and do thingsAll animals can move(), eat(), drink(), hide(), etc.Since these are identical or similar, it makes sense to have a default move() method, a default drink() method, etc.If you have a default draw() method, what would it draw?Since you probably never want an Animal object, but just specific animals (Zebra, Lion, etc.), you don’t need to be able to instantiate the Animal classMake Animal abstract, with an abstract void draw() method6Interfacesinterface MyInterface extends SomeOtherInterface {...}An interface can be public or packageAn interface cannot be finalA class can implement any number of interfacesAn interface can declare (not define) methodsAll declared methods are implicitly public and abstractAn interface can define fields, classes, and interfacesFields are implicitly static, final, and publicClasses are implicitly static and publicAn interface cannot declare constructorsIt’s OK (but unnecessary) to explicitly specify implicit attributes7Declarations and assignmentsSuppose class Cat extends Animal implements Pet {...} and class Persian extends Cat {...} and Cat puff = new Cat();Then the following are true:puff instanceof Cat, puff instanceof Animal, puff instanceof PetThe following is not true: puff instanceof PersianTo form the negative test, say !(puff instanceof Persian)The following declarations and assignments are legal:Animal thatAnimal = puff;Animal thatAnimal = (Animal)puff; // same as above, but explicit upcastPet myPet = puff; // a variable can be of an interface typePersian myFancyCat = (Persian)puff; // does a runtime checkThe following is also legal:void feed(Pet p, Food f) {...} // interface type as a parameter8What are interfaces for?Inheritance lets you guarantee that subclass objects have the same methods as their superclass objectsInterfaces let you guarantee that unrelated objects have the same methodsProblem: Your GUI has an area in which it needs to draw some object, but you don’t know yet what kind of object it will beSolution:Define a Drawable interface, with a method draw()Make your tables, graphs, line drawings, etc., implement Drawable In your GUI, call the object’s draw() method (legal for any Drawable object)If you didn’t have interfaces, here’s what you would have to do:if (obj instanceof Table) ((Table)obj).draw();else if (obj instanceof Graph) ((Graph)obj).draw();else if (obj instanceof LineDrawing) ((LineDrawing)obj).draw(); // etc.Worse, to add a new type of object, you have to change a lot of code9Inner Classes IInner classes are classes declared within another classA member class is defined immediately within another classA member class may be staticA member class may be abstract or final (but not both)A member class may be public, protected, package, or privateA local class is declared in a constructor, method, or initializer blockA local class may be abstract or final (but not both)A local class may access only final variables in its enclosing codeAn anonymous class is a special kind of local class10Inner Classes IIAn anonymous inner class is a kind of local classAn


View Full Document

Penn CIT 591 - Everything You Ever Wanted To Know About Java

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 Everything You Ever Wanted To Know About Java
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 Everything You Ever Wanted To Know About Java 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 Everything You Ever Wanted To Know About Java 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?