Everything You Ever Wanted To Know About Java O O Jan 13 2019 What 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 them 2 Classes 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 class 3 Why inheritance Java provides a huge library of pre written classes 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 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 Your new class really is a more specific kind of the superclass and 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 case 4 What 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 method 5 Interfaces 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 An interface can define fields classes and interfaces All declared methods are implicitly public and abstract 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 attributes 6 Declarations 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 parameter 7 What 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 code 8 Inner 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 class 9 Inner Classes II An anonymous inner class is a kind of local class An anonymous inner class has one of the following forms new NameOfSuperclass parameters class body new NameOfInterface class body Anonymous inner classes cannot have explicit constructors A static member class is written inside another class but is not actually an inner class A static member class has no special access to names in its containing class To refer to the static inner class from a class outside the containing class use the syntax OuterClassName InnerClassName A static member class may contain static fields and methods 10 What are inner classes for Sometimes a class is needed by only one other class Sometimes a class needs access to many variables and methods of another class Example A class to handle an event such as a button click is probably needed only in the GUI class Having such a class available at the top level where it isn t needed just adds clutter It s best to hide such classes from other classes that don t care about it Again an event handler is a good example Making it an inner class gives it full
View Full Document
Unlocking...