Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Programming with Java 1 Chapter 12 More OOP, Interfaces, and Inner Classes © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 2 Objectives • Create your own interfaces. • Identify the types of inner classes available in Java. • Understand and use inner classes. © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 3 Encapsulation • The term encapsulation refers to the fact that all methods and variables are declared inside of a class, which it can hide or expose, as necessary. • When you declare variables and methods private, no other classes have access to the data. © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 4 Inheritance • Inheritance provides the ability to create a class based on another class. • You gain all the functionality of a superclass (also known as base class) and add your own functionality. • One superclass can have several subclasses that inherit from it. • A subclass on the other hand can only inherit from one superclass. © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 5 Hierarchy of Classes © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 6 Polymorphism • Many methods have the same name such as setText, which can be used for a label as well as for a text component. • You can strive for the same thing, for example in having different constructors. © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 7 Inheriting from Your Own Classes • So far we have inherited classes that only exist in the Java language. • You can extend a class that you have written. • Often the technique of inheritance is used intentionally at design stage to keep similar objects with a single superclass. • Consider a profile of a person : Last Name, First Name, Street, City, State, …. • A profile of person can be used for employee, customer, or vendor. • We can create a superclass of a Person then inherit subclasses for employee, vendor, or customer. © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 8 Superclass and Subclasses © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 9 Protected Variables • Private variables are not inherited. • If you plan to inherit from a class, you should make the variables either public or protected. • Variables declared as protected act as private in the superclass, but can be inherited to a subclass. © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 10 Interfaces • Interfaces are Java’s answer to its lack of multiple inheritance. • The implements clause can list several interfaces. • Your program can have access to the methods and constants in many interfaces. • Interfaces have several similarities to classes. Each is a separate file saved with a .java extension. Both contain coding to declare variables and methods. • Certain limitations apply to interfaces: Methods do not contain a body. All variables must be declared as final (constants). © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 11 Abstract Classes and Methods • An abstract class is strictly written for inheritance. • You cannot instantiate an object of an abstract class, but you can inherit from the class. • Abstract classes can contain methods with code along with other methods that are empty, which are called abstract methods. • This requires a subclass to override an empty class by including a method with the same name. • You also have the option of overriding any other non-abstract methods in the base class. © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 12 Abstract Classes and Methods Continued • You can declare a class to be abstract by adding the keyword abstract in the class header. • If you include any abstract (empty) methods in the class, the class must be declared abstract. • An easy error to generate is to accidentally place a semicolon at the end of the line declaring a new method. • Java considers this an abstract method and warns you that you must declare the class abstract. © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 13 Interface Abstract Methods • The methods in the interface are public by default, whether or not you include the keyword public. • All fields will be public, final and static, whether or not you include the keywords. • However, it is good practice to include the keywords. © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 14 Creating an Interface • You can create your own interface using the keyword interface instead of class. • You can now refer to the constants in any application or applet that implements the interface. © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 15 Inner Classes • • • • • • An inner class is class that is declared in another class. Some refer to this as a nested class. Why would you want to declare a class inside another class? The most common reason is to organize code. You can also take advantage of scoping rules for variables and methods. Code within the inner class can directly reference variables and methods within the scope of the class – the code in the outer class can directly reference variables and methods inside the inner class. An important thing is writing inner classes are merely a different style of coding. © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 16 Inner Classes Type of Inner Class Applies To Declared Can be Used Static Member Classes and interfaces Inside a class as static By any class Member (nonstatic) Classes Inside a class (non-static) Within the member class Local (named) Classes Inside a method Within the method Anonymous (local unnamed) Classes Inside a method with no name Within the method © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 17 Inner Classes © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 18 Static Member Classes and Interfaces • You can declare an inner member class to be static. • Just as the class variables, the outer class has only one copy of the inner class, not a copy per instance. • You can declare a static inner class to keep summary information. • A static member class follows the scoping rules for a class and instance variables. © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 19 Static Member Classes and Interfaces Continued • You declare the class at the top level (not in a method), and it can be referenced by code anywhere inside the class. • A static member class can access all of the static members of its containing class, including private members. • In a program with many methods, it is difficult to determine the ones that overriding from the interface and/or superclass and those that belong specifically to the class. © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 20 Member Classes • • • • • • A member class is an inner class that is not declared static and also not declared inside a method. A member class – one copy exists for each instance of the class. The inner class only exists for the use of the outer class and cannot be used by other top-level classes. The nested class has access to all instance and class variables and objects of the outer class or any other inner classes, including members declared as private. The outer class has access to all the variables and methods in the inner class. An inner class declared in an applet has access to all of the components. © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 21 Local Classes • A local inner class is an inner class declared inside a method. • Local inner classes are closely related to the local variables, which have a local scope and can be used only within that method. • A common use of the local inner class is to implement an event handling interface or extend another class, such as the MouseAdapter or WindowAdapter. • Since the code for the event handling appears in only one method, it makes sense to code it as a local inner class. © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 22 Anonymous Classes • An anonymous class is a local class that you define without giving it a name. • You can declare an anonymous inner class as an expression, which may be an argument for a method call or assigned to a variable. • If you declare a local class (with a name), you define the class and then instantiate an object of that class. • With an anonymous class, you won't be referring to the class object by name, so you can combine the two steps (definition and instantiation). © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 23 Anonymous Classes Continued For example (used in Chapter 10 and 11) frmMain.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { //Action for window close box System.exit(0); } }); © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Programming with Java 24 Distributing Inner Classes • When you distribute an applet or application that uses inner classes, you must make sure to include all .class files. • Although you write one class inside another, the compiler produces separate .class files for each class. • For example, the PayrollApplet has only one .java file— PayrollApplet.java, but the compiler generates two .class files—PayrollApplet.class and PayrollApplet$Payroll.class. © 2002 The McGraw-Hill Companies, Inc. All rights reserved.