License Lesser General Public License, version 2.1
Lines 42
Keywords
inner class (1) inner interface (1)
Included in this Library
Permissions
Owner: Stou S.
Viewable by Everyone
Editable by All Siafoo Users

Local inner class and inner interface example Atom Feed 0

In Brief An example of using the an inner class in Java, that is a class defined inside a method or another class. It also illustrates how to define an inner interface.
# 's
 1//package com.wickedcooljava.core;
2
3/*
4 Example code from Wicked Cool Java (No Starch Press)
5 Copyright (C) 2005 Brian D. Eubanks
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
21 Note: The LGPL licence can be found online at http://www.gnu.org
22
23 */
24/**
25 * This example demonstrates how to create a local inner class, and an inner
26 * interface.
27 */
28public class Happiness {
29 interface Smiler {
30 public void smile();
31 }
32
33 public static void main(String[] args) {
34 class Happy implements Smiler {
35 private String more = "";
36
37 public void smile() {
38 System.out.println(":-)" + more);
39 }
40
41 public void happier() {
42 more += ")";
43 }
44 }
45
46 Happy h1 = new Happy();
47 h1.smile();
48 Happy h2 = new Happy();
49 h2.happier();
50 h2.smile();
51 }
52}

An example of using the an inner class in Java, that is a class defined inside a method or another class. It also illustrates how to define an inner interface.