ch7

Cards (36)

  • Interface
    Similar to a class but with restrictions: all methods are implicitly abstract and public, cannot have instance variables (but can have constants), can extend other interfaces, and is implemented by a class using the 'implements' keyword
  • Interfaces
    • All methods are implicitly abstract and public
    • Cannot have instance variables (but can have constants)
    • Can extend other interfaces
    • Implemented by a class using the 'implements' keyword
  • Java does not allow for multiple inheritance, but interfaces can be used to achieve similar advantages
  • An interface is a type that groups together a number of different classes that all include method definitions for a common set of method headings
  • An interface specifies the headings for methods that must be defined for any class that implements the interface
  • An interface contains method headings and constant definitions only, no instance variables or complete method definitions
  • Interfaces are implicitly abstract, and all methods in an interface are implicitly abstract and public
  • Implementing an Interface Type
    Include 'implements Interface_Name' in class definition
    2. Implement all method headings listed in the interface definitions
  • Converting Between Class and Interface Types
    Can convert from a class type to an interface type if the class implements the interface, but cannot convert between unrelated types
  • Narrowing the scope of the overridden methods in the implementing class is a common error
  • Inner (or Nested) Classes
    Classes defined/declared within other classes or interfaces, where the class that includes the inner class is called the outer class
  • Within the definition of a method of an inner class, it is legal to reference a private instance variable of the outer class or invoke a private method of the outer class
  • Within the definition of a method of the outer class, it is legal to reference a private instance variable of the inner class on an object of the inner class or invoke a non-static method of the inner class
  • Four categories of Java inner classes
    • Non-static inner classes: Member inner classes, Local classes, Anonymous classes
    Static nested classes
  • Member Inner Class
    A non-static class that is created inside a class but outside a method
  • Within the definition of a method of the outer class, it is legal to reference a private instance variable of the inner class on an object of the inner class
  • Within the definition of a method of the outer class, it is legal to invoke a (non-static) method of the inner class as long as an object of the inner class is used as a calling object
  • Within the definition of the inner or outer classes, the modifiers public and private are equivalent
  • Four categories of Java inner classes
    • Non-static inner classes
    • Member inner classes
    • Local classes (defined inside a block of Java code)
    • Anonymous classes (defined inside a block of Java code)
    • Static nested classes
  • Member Inner Class

    A class created within class and outside method
  • Local Inner Class

    A class created within method
  • Anonymous Inner Class

    A class created for implementing interface or extending class. Its name is decided by the Java compiler
  • Static Nested Class
    A static class created within class
  • Nested Interface
    An interface created within class or interface
  • static is used for a constant, variable or a method that is same for every instance of a class
  • Member Inner Class
    • A non-static class that is created inside a class but outside a method
    • If an inner class is marked with the public modifier instead of private, then it can be used outside of the outer class
    • To create an object of the non-static inner class, you must start with an object of the outer class
    • For a static inner class, you can create objects of a public static inner class and do so outside of the inner class—in fact, even outside of the outer class
    • If both the inner and outer classes have a method with the same name, and the intent is to invoke the method in the outer class, then the following invocation must be used: OuterClassName.this.methodName()
  • Nesting inner classes within inner classes
    • The rules are the same as before, but the names get longer
  • Inner Classes and Inheritance
    • Given an OuterClass that has an InnerClass. If you derive DerivedClass from OuterClass, then DerivedClass automatically has InnerClass as an inner class just as if it were defined within DerivedClass
  • Local Inner Class
    • A class that is created inside a method
    • Local inner class cannot be invoked from outside the method
    • If you want to invoke the methods of local inner class, you must instantiate this class inside the method
    • Local classes are completely hidden in their containing block
    • A local class can access instance variables of the outer class and any local variable [only the final local variables (till jdk 1.7 only)] of the enclosing block
  • Anonymous Inner Class

    • A class that has no name
    • It should be used if you have to override method of class or interface
    • It can be created by two ways: 1. Class (may be abstract or concrete), 2. Interface
    • The class definition is embedded inside the expression with the new operator
    • Anonymous class has no constructors
    • It is either derived from a class or implements an interface
  • Static Nested Class
    • A static class created inside a class
    • It cannot access non-static data members and methods
    • It can be accessed by outer class name
    • It can access static data members of outer class including private
    • Static nested class cannot access non-static (instance) data member or method
    • If you have the static member inside static nested class, you don't need to create instance of static nested class
  • Compiling any class in Java produces a (.class) file named: ClassName.class
  • Compiling a class with one or more inner classes causes both (or more) classes to be compiled and produces two or more (.class) files such as: ClassName.class and ClassName$InnerClassName.class
  • Often, instead of having one action listener object deal with all the action events in a GUI, a separate ActionListener class is created for each button or menu item
  • Each button or menu item has its own unique action listener
  • When this approach is used, each class is usually made a private inner class