Object Oriented Programming in Java Habib Rostami Lecture 10.

Slides:



Advertisements
Similar presentations
Inner Classes. Nested Classes  An nested class is a class that is defined inside another class.  To this point we have only studied top-level classes.
Advertisements

Local Variables and Scope Benjamin Fein. Variable Scope A variable’s scope consists of all code blocks in which it is visible. A variable is considered.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 27 - Java Object-Oriented Programming Outline.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
Java & Inner Classes L. Grewe. Kinds of of Classes Top level classes Top level classes Declared inside packageDeclared inside package Visible throughout.
Event-Driven Programming
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
System Architecture Lecture 3 CSE 111 Spring /22/20151Copyright William E. Howden.
Inner Classes. Lecture Objectives Learn about inner classes. Know the differences between static and non- static inner classes. Designing and using inner.
OOP in Java – Inner Classes Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Inner Classes. Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class Inner classes.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
Abstract and Nested Classes
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
2-Dec-15 Inner Classes. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class.
Java Inner Classes. Interfaces interface KeyListener { public void keyPressed(KeyEvent e); public void keyReleased(KeyEvent e); public void keyTyped(KeyEvent.
2-Dec-15 Inner Classes By Alguien Soy. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside.
Object Oriented Programming.  Interface  Event Handling.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Nested Classes CompSci 230 S Software Construction.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Duke CPS Modules in Java l Classes that are related should be grouped together, may even share access to otherwise private methods/instance variables.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Event Handling CS 21a: Introduction to Computing I First Semester,
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Inner Classes.
CSC 205 Programming II Lecture 5 AWT - I.
Topic: Inner Classes Course : JAVA PROGRAMMING Paper Code: ETCS-307 Faculty : Dr. Prabhjot Kaur Reader, Dept. of IT 1.
Inner Classes 27-Dec-17.
CompSci 230 S Software Construction
CompSci 230 S Programming Techniques
Ellen Walker Hiram College
Nested class.
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Chapter 15 Event-Driven Programming and Animations
Overloading and Overriding
Inner Classes 29-Nov-18.
Interfaces.
Java Inner Classes.
Constructors, GUI’s(Using Swing) and ActionListner
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Inner Classes 1-May-19.
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Chapter 15 Event-Driven Programming and Animations Part 1
Inner Classes.
...that can get messy when we have lots buttons!
Inner Classes 25-Oct-19.
Presentation transcript:

Object Oriented Programming in Java Habib Rostami Lecture 10

Inner class An inner class is a class declared inside another class –The class that encloses it is called the outer or top-level class. A “normal class” is a direct member of a package; inner classes are not.

Types of inner classes Static member classes Member classes Local classes Anonymous classes

Types of inner classes Static member classes –Is static, has access to all static methods of the outer class Member classes –Is instance-specific and has access to any and all methods and members, even the parent's this reference.

Types of inner classes Local classes –declared within a block of code and are visible only within that block, just as any other method variable. Anonymous classes –A class with no name –More on this later…

Inner classes When inner classes are compiled, the compiler generates classes of this format: –ContainingClass$InnerClass.class If you want to distinguish between the inner class's methods/fields and that of the containing class, use "ContainingClass.this" –e.g., in PersonMoverApplet6's MoverButton inner class, we say PersonMoverApplet6.this.repaint()

Anonymous classes A class with no name, which is why it’s called an “anonymous class” Combines the following into one step: –class declaration –creation of an instance of the class Anonymous objects cannot be instantiated from outside the class in which the anonymous class is defined –it can only be instantiated from within the same scope in which it is defined

Why use an anonymous class? It lessens the amount of “.java” files necessary to define the application. –anonymous classes can access the static and instance variables of the enclosing outer class. Can be time-savers Useful when implementing listeners in GUI programs –See sample code in access folder of slide7.zip

Anonymous classes When you compile classes that contain anonymous classes, the compiler will create these class files: –ClassName$SomeNumber –SomeNumber is the sequence number for the anonymous class So, if you have a class named MyAnonTest.java that contains two anonymous classes, the following class files will be generated by the compiler: –MyAnonTest.class, MyAnonTest$1.class and MyAnonTest$2.class

Example Usual way: add a listener for each component and define the action inside the actionPerformed() method –Would have a very long if-else chain if many buttons used With anonymous classes, you can directly add and define the action on the component button.addActionListener( new ActionListener() { //anonymous inner class for WindowAdapter public void actionPerformed(ActionEvent ae) { System.out.println("Clicked!"); } });

Rules for Anonymous classes An anonymous class must always extend a super class or implement an interface but it cannot have an explicit extends or implements clause An anonymous class must implement all the abstract methods in the super class or the interface. An anonymous class always uses the default constructor from the super class to create an instance

When to use Inner Classes In general –Use when you do NOT need to reuse or extend the class outside the containing class. –e.g., for handling events that are specific to that applet Non-Anonymous Inner Classes –Use when you create several different instances of the same inner class within the same class Anonymous Inner Classes –Use when you only need one instance of that class and never need to reuse it.