Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
CSC 1214: Object-Oriented Programming J. Kizito Makerere University BSc (CS, M) e-mail: www: materials: e-learning environment: office: alt. office: – MUK; MSc (S/W & Sys Eng) – RuG [email protected] http://serval.ug/~jona http://serval.ug/~jona/materials/CSC1214 http://muele.mak.ac.ug block A, level 3, wing B, rm 304B institute of open, distance, and eLearning, rm D20 Interfaces, Nested and Inner classes Kizito (Makerere University) CSC 1214 April, 2017 1 / 16 Overview 1 Interfaces Introduction Implementing Interfaces Extending Interfaces 2 Nested and Inner classes Inner Classes Local Classes Anonymous Classes Kizito (Makerere University) CSC 1214 April, 2017 2 / 16 Interfaces Introduction Interfaces Recall: “If an abstract class contains only abstract method declarations, it should be declared as an interface instead” Thus an interface is an abstract class with only abstract methods General form: access interface name { type final-varname1 = value; type final-varname2 = value; // ... type final-varnameN = value; return-type method-name1(parameter-list); return-type method-name2(parameter-list); // ... return-type method-nameN(parameter-list); } Kizito (Makerere University) CSC 1214 April, 2017 3 / 16 Interfaces Implementing Interfaces Interfaces Implementing Interfaces An interface is a reference type, similar to a class, that can contain only constants, method signatures, and nested types – no method bodies Interfaces cannot be instantiated – they can only be implemented by classes or extended by other interfaces To implement an interface, include the implements clause in a class definition, and implement the methods defined Although the Java programming language does not permit multiple inheritance, interfaces are not part of the class hierarchy and thus allow us to implement more than one interface at the same time General form: access class classname [extends superclass] // in case of Inheritance [implements interface[, interface]] { // class-body } Kizito (Makerere University) CSC 1214 April, 2017 4 / 16 Interfaces Implementing Interfaces Interfaces Examples1 3 1 public interface Animal { public void makeSound(); public void move(); } public interface MobileMoney { public boolean ReceiveMoney( String to, String from, float amt); } 2 public interface EmailServer { public final String SERVER REF = "2013.001"; public boolean ReceiveMail(String to, String from, String subject, String body public String getHostName(); } 1 Engineer Bainomugisha, PhD Kizito (Makerere University) CSC 1214 April, 2017 5 / 16 Interfaces Implementing Interfaces Interfaces Examples1 3 1 public interface Animal { public void makeSound(); public void move(); } public interface MobileMoney { public boolean ReceiveMoney( String to, String from, float amt); } 2 public interface EmailServer { public final String SERVER REF = "2013.001"; public boolean ReceiveMail(String to, String from, String subject, String body public String getHostName(); } 1 Engineer Bainomugisha, PhD Kizito (Makerere University) CSC 1214 April, 2017 5 / 16 Interfaces Implementing Interfaces Interfaces Examples1 3 1 public interface Animal { public void makeSound(); public void move(); } public interface MobileMoney { public boolean ReceiveMoney( String to, String from, float amt); } 2 public interface EmailServer { public final String SERVER REF = "2013.001"; public boolean ReceiveMail(String to, String from, String subject, String body public String getHostName(); } 1 Engineer Bainomugisha, PhD Kizito (Makerere University) CSC 1214 April, 2017 5 / 16 Interfaces Implementing Interfaces Interfaces Example (1) – Implementing Interfaces 1 (a) public class Cat implements Animal { public void makeSound () { System.out.println("Meow"); } public void move () { /* implementation */ } // Any additional methods. } 1 (b) public class Dog implements Animal { public void makeSound () { System.out.println("Woof"); } public void move () { /* implementation */ } // Any additional methods. } Kizito (Makerere University) CSC 1214 April, 2017 6 / 16 Interfaces Implementing Interfaces Interfaces Example (1) – Implementing Interfaces 1 (a) public class Cat implements Animal { public void makeSound () { System.out.println("Meow"); } public void move () { /* implementation */ } // Any additional methods. } 1 (b) public class Dog implements Animal { public void makeSound () { System.out.println("Woof"); } public void move () { /* implementation */ } // Any additional methods. } Kizito (Makerere University) CSC 1214 April, 2017 6 / 16 Interfaces Implementing Interfaces Interfaces Example (2) – Implementing Interfaces 2 (a) public class YahoomailServer implements EmailServer { public boolean ReceiveMail(String to, String from, String subject, String body) { /* implementation */ } public String getHostName() { /* implementation */ } } 2 (b) public class GmailServer implements EmailServer { public boolean ReceiveMail(String to, String from, String subject, String body) { /* implementation */ } public String getHostName() { /* implementation */ } } Kizito (Makerere University) CSC 1214 April, 2017 7 / 16 Interfaces Implementing Interfaces Interfaces Example (2) – Implementing Interfaces 2 (a) public class YahoomailServer implements EmailServer { public boolean ReceiveMail(String to, String from, String subject, String body) { /* implementation */ } public String getHostName() { /* implementation */ } } 2 (b) public class GmailServer implements EmailServer { public boolean ReceiveMail(String to, String from, String subject, String body) { /* implementation */ } public String getHostName() { /* implementation */ } } Kizito (Makerere University) CSC 1214 April, 2017 7 / 16 Interfaces Implementing Interfaces Interfaces Example (3) – Implementing Interfaces 3 (a) public class MTNMobileMoney implements MobileMoney { public boolean ReceiveMoney(String to, String from, float amt) { /* implementation */ } // Additional methods public void MTNBonus() { /* implementation */} } 3 (b) public class AirtelMoney implements MobileMoney { public boolean ReceiveMoney(String to, String from, float amt) { /* implementation */ } // Additional methods public void EnterAirtelDraw() { /* implementation */} } Kizito (Makerere University) CSC 1214 April, 2017 8 / 16 Interfaces Implementing Interfaces Interfaces Example (3) – Implementing Interfaces 3 (a) public class MTNMobileMoney implements MobileMoney { public boolean ReceiveMoney(String to, String from, float amt) { /* implementation */ } // Additional methods public void MTNBonus() { /* implementation */} } 3 (b) public class AirtelMoney implements MobileMoney { public boolean ReceiveMoney(String to, String from, float amt) { /* implementation */ } // Additional methods public void EnterAirtelDraw() { /* implementation */} } Kizito (Makerere University) CSC 1214 April, 2017 8 / 16 Interfaces Implementing Interfaces Interfaces Example (4) – A stack interface // Define an integer stack interface interface IntStack { void push(int item); // store an item int pop(); // retrieve an item } // An implementation of IntStack that uses fixed storage class IFTest { class FixedStack implements IntStack { public static void main(String args[]) { private int stck[]; Stack mystack1 = new FixedStack(5); private int tos; Stack mystack2 = new FixedStack(8); // allocate and initialize stack Stack(int size) { // push some numbers onto the stacks Stck = new int[size]; for (int i = 0; i < 5; i++) tos = -1; mystack1.push(i); } for (int i = 0; i < 8; i++) // Push an item onto the stack mystack2.push(i); void push(int item) { if (tos==stck.length-1) // pop those numbers from the stacks System.out.println("Stack is full."); System.out.println("Stack in mystack1:"); else for (int i = 0; i < 5; i++) stck[++tos] = item; System.out.println(mystack1.pop()); } System.out.println("Stack in mystack2:"); // Pop an item from the stack for (int i = 0; i < 8; i++) int pop() { System.out.println(mystack2.pop()); if (tos < 0) { } System.out.println("Stack underflow."); } return 0; } else return stck[tos--]; } } Kizito (Makerere University) CSC 1214 April, 2017 9 / 16 Interfaces Implementing Interfaces Interfaces Example (4) – A stack interface // Define an integer stack interface interface IntStack { void push(int item); // store an item int pop(); // retrieve an item } // An implementation of IntStack that uses fixed storage class IFTest { class FixedStack implements IntStack { public static void main(String args[]) { private int stck[]; Stack mystack1 = new FixedStack(5); private int tos; Stack mystack2 = new FixedStack(8); // allocate and initialize stack Stack(int size) { // push some numbers onto the stacks Stck = new int[size]; for (int i = 0; i < 5; i++) tos = -1; mystack1.push(i); } for (int i = 0; i < 8; i++) // Push an item onto the stack mystack2.push(i); void push(int item) { if (tos==stck.length-1) // pop those numbers from the stacks System.out.println("Stack is full."); System.out.println("Stack in mystack1:"); else for (int i = 0; i < 5; i++) stck[++tos] = item; System.out.println(mystack1.pop()); } System.out.println("Stack in mystack2:"); // Pop an item from the stack for (int i = 0; i < 8; i++) int pop() { System.out.println(mystack2.pop()); if (tos < 0) { } System.out.println("Stack underflow."); } return 0; } else return stck[tos--]; } } Kizito (Makerere University) CSC 1214 April, 2017 9 / 16 Interfaces Implementing Interfaces Interfaces Partial implementations If a class does not implement all of the interface’s methods, then it must be declared as abstract What if a class implements two interfaces with the same method name? abstract class FixedStack implements IntStack { private int stck[], tos; Stack(int size) { stck = new int[size]; tos = -1; } void push(int item) { if (tos==stck.length-1) System.out.println("Stack is full."); else stck[++tos] = item; } } Kizito (Makerere University) CSC 1214 April, 2017 10 / 16 Interfaces Extending Interfaces Interfaces Interfaces can be extended interface A { void meth1(); void meth2(); } inteface B extends A { void meth3(); } class MyClass implements B { // This class MUST implement all of A and B } Kizito (Makerere University) CSC 1214 April, 2017 11 / 16 Nested and Inner classes Nested and Inner classes It is possible to define a class within another class A nested class has access to members (including private) of the enclosing class The enclosing class does not have access to the members of the nested class An inner class is a non-static nested class Nested classes in C++ are equivalent to the static inner classes in Java Unlike a C++ nested class, an instance of a Java inner class contains an implicit, hidden reference to an instance of the outer class. (Note that this reference is real for garbage collection purposes) Kizito (Makerere University) CSC 1214 April, 2017 12 / 16 Nested and Inner classes Inner Classes Nested and Inner classes Example (5) class Outer { int outer x = 100; void test() { Inner inner = new Inner(); inner.display(); } class Inner { void display() { System.out.println("outer x=" + outer x); } } } Kizito (Makerere University) CSC 1214 April, 2017 13 / 16 Nested and Inner classes Local Classes Nested and Inner classes Nesting a class inside a method These inner classes, also called local classes, cannot have access modifiers, like local variables, since the class is ’private’ to the method The inner class can only be abstract or final In addition to instance variables of the enclosing class, local classes can also access local variables of the enclosing method, but only ones that are declared final public class OuterClass { public void method() { class InnerClass { // ... } } } Kizito (Makerere University) CSC 1214 April, 2017 14 / 16 Nested and Inner classes Anonymous Classes Anonymous Classes In Java, a class definition and its instantiation can be combined into a single step These classes, that require no name, are called anonymous classes Anonymous class is a special case of a class local to a method; hence they also can access final local variables of the enclosing method Anonymous classes are most useful when creating an instance of an interface or adapter class without the need for a brand new class Using anonymous classes is especially preferable when you intend to use many different classes that each implement the same interface public interface ActionListener { public void actionPerformed(); } ActionListener listener = new ActionListener() { public void actionPerformed() { // Implementation of the action event // ... return; } }; Kizito (Makerere University) CSC 1214 April, 2017 15 / 16 Nested and Inner classes Anonymous Classes Anonymous Classes Example (6) – Implementing the Action Listener Anonymous inner classes are particularly useful in Event Handling – Chapter 20 of The Complete Reference – JavaTM 2, by Patrick Naughton & Herbert Schildt, Fifth Edition As already mentioned, we plan not to cover this topic at the moment The following examples both implement the ActionListener interface on previous slide and thus do the same thing! MyApp.java – using a named class MyApp.java – using an anonymous inner class import java.awt.Button; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.Button; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; class MyApp { Button aButton = new Button(); class MyApp { Button aButton = new Button(); // Nested class to implement the action listener class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("Hello There"); } } MyApp() { MyActionListener al = new MyActionListener(); aButton.addActionListener(al); } MyApp() { aButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Hello There"); } } ); } } } Kizito (Makerere University) CSC 1214 April, 2017 16 / 16