1 / 11

Inner Classes

Inner Classes. An Inner Class is a class defined inside of another class (Static) Inner Classes are a lot like C++ ’ s nested classes Since Java 1.1, Java now has Inner Classes

lang
Download Presentation

Inner Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inner Classes • An Inner Class is a class defined inside of another class • (Static) Inner Classes are a lot like C++’s nested classes • Since Java 1.1, Java now has Inner Classes • Originally, the Java authors decided not to include them because they thought they would needlessly complicate the language. • However, they can be very useful, especially with AWT (GUI) event handling.

  2. Inner Classes • An Inner class is a class defined inside of another class. Why would anyone want to do that? • An Inner class object can access the implementation of the object that created it – including private fields • Inner classes can be hidden from other classes in the same package • Anonymous inner classes are frequently used for creating event callbacks

  3. How to declare an inner class • You declare an inner class almost like a method inside of a class class ICExample { private class thisIsAnInnerClass //THIS IS THE INNER CLASS { // define a class here } }

  4. Visibility Rules • Inner classes are members of the outer class • Can be declared with any visibility (public, private, etc.) • Can see all members (including private) of outer class • Is in the same package as outer class (outer class can access all non-private inner class members) • Typically, the inner class is declared private and the inner classes members are either public or package visible.

  5. outer keyword • With inner classes, Java defines a outer class reference with each inner class which can be accessed with the outer keyword //this is from the BankAccount class on page 216 of Core Java volume I public void actionPerformed(ActionEvent e) //inside the inner class { double interest=outer.balance * this.interestRate; outer.balance += interest; } • The balance field is a private member of the outer class.

  6. Static inner classes • Objects of a static inner class can exist even without an object of the enclosing outer class. • Because of this, a static inner class is not allowed to access fields of the outer object

  7. Local inner classes • You can actually declare an inner class inside of a method, just like you could declare a local variable. • Local classes do not get an access modifier – they are automatically restricted to the method they are defined in • Can only refer to final members of the enclosing class

  8. Anonymous Inner classes • When using a local inner class, if you only want to make one instance of it, you don’t even need to give it a name • This is known as an anonymous inner class • These are convenient for event programming • However, the syntax is extremely cryptic. Look at this example

  9. Anonymous Inner classes public void start(final double rate) { ActionListener adder = new ActionListener() { public void actionPerformed(ActionEvent evt) { double interest = balance * rate / 100; balance += interest; } }; Timer t = new Timer(1000, adder); t.start(); • This is saying, construct a new object of a class that implements the ActionListener interface, where the one required method (actionPerformed) is defined inside the brackets.

  10. Anonymous Inner classes • You have to look very carefully to see a difference between construction of a new object, and construction of a new inner class extending a class. //A person object Person queen=new Person(“Mary”); //Person Object //An object of an inner class extending Person Person count = new Person(“Dracula”) { //class code here};

  11. Anonymous Inner classes • Anonymous Inner classes cannot have constructors, since constructors have to have the same name as the class, and these classes have no names. • As you can see, the syntax for these is confusing – both for people writing and reading the code. Use this with care, if at all.

More Related