Abstract Classes and Methods Interface

J.E.D.I. It produces the following output. sub: 3 sub: 4 The method called is determined by the actual data type of the object that invoked the method. The access modifier for the methods need not be the same. However, the access modifier of the overriding method must either have the same access modifier as that of the overridden method or a less restrictive access modifier. Consider the next example. Check out which of the following overridding methods would cause a compile time error to occur. class Superclass { void overriddenMethod { } } class Subclass1 extends Superclass { public void overriddenMethod { } } class Subclass2 extends Superclass { void overriddenMethod { } } class Subclass3 extends Superclass { protected void overriddenMethod { } } class Subclass4 extends Superclass { private void overriddenMethod { } } The overriddenMethod in Superclass has the defaultpackage access modifier. The only modifier more restrictive than this is private. Thus, Subclass4 causes a compiler error because it tries to override overriddenMethod in Superclass with a more restrictive modifier private.

1.3.12 Abstract Classes and Methods

The general form for an abstract method is as follows: abstract modifier returnType nameparameter; A class containing an abstract method should be declared as an abstract class. abstract class name { constructors, fields and methods } The keyword cannot be applied to a constructor or a static method. It also important to remember that abstract classes cannot be instantiated. Introduction to Programming II Page 17 J.E.D.I. Classes that extends an abstract class should implement all abstract methods. If not the subclass itself should be declared abstract. Coding Guidelines: Note that declaring an abstract method is almost similar to declaring a normal class except that an abstract method has no body and the header is immediately terminated by a semicolon ;. For example: abstract class SuperHero { String superPowers[]; void setSuperPowersString superPowers[] { this.superPowers = superPowers; } void printSuperPowers { for int i = 0; i superPowers.length; i++ { System.out.printlnsuperPowers[i]; } } abstract void displayPower; } class UnderwaterSuperHero extends SuperHero { void displayPower { System.out.printlnCommunicate with sea creatures...; System.out.printlnFast swimming ability...; } } class FlyingSuperHero extends SuperHero { void displayPower { System.out.printlnFly...; } }

1.3.13 Interface

Declaring an interface is basically declaring a class but instead of using the class keyword, the interface keyword is used. Here is the syntax. interfaceDeclaration ::= modifier interface name { attributeDeclaration [modifier returnType nameparameter;] } Members are public when the interface is declared public. Coding Guidelines: Attributes are implicitly static and final and must be initialized with a constant value. Like in declaring a top-level class, the only valid access modifiers are public and package i.e., if no access modifier prefixes the class keyword. A class can implement an existing interface by using the implements keyword. This class is forced to implement all of the interfaces methods. A class may implement more than one interface. Introduction to Programming II Page 18 J.E.D.I. The following example demonstrates how to declare and use an interface. interface MyInterface { void iMethod; } class MyClass1 implements MyInterface { public void iMethod { System.out.printlnInterface method.; } void myMethod { System.out.printlnAnother method.; } } class MyClass2 implements MyInterface { public void iMethod { System.out.printlnAnother implementation.; } } class InterfaceDemo { public static void mainString args[] { MyClass1 mc1 = new MyClass1; MyClass2 mc2 = new MyClass2; mc1.iMethod; mc1.myMethod; mc2.iMethod; } } Here is the expected output: Interface method. Another method. Another implementation.

1.3.14 The this Keyword