SlideServe
Menu
Préférences
© SlideServe · v2026

Inner Classes

smithwick avatar
8 diapositives · 201 Views · Jan 05, 2020

Understand the concept of inner classes, their benefits, and pitfalls. Learn how to create and utilize inner classes effectively. Review considerations for using inner classes in Java programming.

Inner Classes - slide 1
01 / 8Inner Classes
1 / 17

Télécharger la présentation

Inner Classes
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Inner Classes

  2. In this class, we will cover: Inner classes What an inner class is Why you would use an inner class Creating an inner class Using an inner class Dangers of inner classes Review for midterm

  3. Inner Classes • An inner class is class defined within another class. • Why would you want to use inner classes?1. Inner classes can access the implementation of the object that created it - including private data.2. Inner classes can be hidden from other classes in the | same package.3. Anonymous inner classes are handy when you want to define callbacks on the fly.4. Inner classes are very convenient when you are writing event-driven programs.

  4. Creating an Inner Class • See InnerClassTest.java in book. • Special syntax rules for inner classes: • You can use OuterClass.this - not necessary though, it is implied. • e.g. public void actionPerformed(ActionEvent event) { double interest = BankAccount.this.balance * this.rate / 100; BankAccount.this.balance += interest; } • You can also use the object’s name when the inner class occurs outside the scope of the outer class. • e.g. BankAccount mySavings = new BankAccount(10000); BankAccount.InterestAddr adder = mySavings.new InterestAdder(10);

  5. Using an Inner Class to Access Object State • Inner classes are not instance fields of the outer class. • When inner classes construct objects, these objects are local to the methods of the outer class. • With private inner classes, only outer class methods can generate inner class objects. • These inner class objects has access to both its own data fields and the data fields of the outer class fields.

  6. Dangers of Inner Classes • The syntax is complex - unnecessarily so. • Inner classes are genuinely more powerful than regular classes since they have more access privileges - saves some typing. • But you can almost anything you can do with inner classes you can do with regular classes. • It is important to know about inner classes though in case you run across them. • Some would argue that inner classes are more elegant and interesting than they are needed. • Security risks: • The way the compiler handles inner classes exposes private fields and methods to malicious code. • It is relatively easy for someone to manipulate this exposure. • Difficult for programmers to read and maintain.

  7. Summation of Inner Classes • They can be useful for certain situations (like event handling), but use them sparingly. • Be careful when you do use them.

  8. Midterm Review • What have we gone over so far: • Basic syntax of the Java language • Object-oriented terms and concepts • How these concepts translate into code • Inheritance • Interfaces and inner classes