Nested class.

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

Final and Abstract Classes
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
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.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Nested Classes Yoshi Modified from:
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Inner Classes. Lecture Objectives Learn about inner classes. Know the differences between static and non- static inner classes. Designing and using inner.
CS102--Object Oriented Programming Lecture 16: – Inner classes + review Copyright © 2008 Xiaoyan Li.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Chapter 8 More Object Concepts
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Inheritance in the Java programming language J. W. Rider.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Nested Classes CompSci 230 S Software Construction.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Object Oriented Programming in Java Habib Rostami Lecture 10.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
N ESTED C LASSES -1. T YPES OF N ESTED C LASSES & I NTERFACES top-level nested classes interfaces inner classes member local named anonymous.
Inner Classes.
Inner Classes 27-Dec-17.
Programming in Java: lecture 7
Modern Programming Tools And Techniques-I
CompSci 230 S Software Construction
OOP: Encapsulation &Abstraction
Inheritance and Polymorphism
CompSci 230 S Programming Techniques
Object Oriented Programming
Understanding Inheritance
Android Programming Lecture 2
Chapter 9 Inheritance and Polymorphism
Java Programming Language
Overloading and Overriding
Packages and Interfaces
Inner Classes 29-Nov-18.
Advanced Java Programming
Java – Inheritance.
CMSC 202 Inner Classes.
Java Programming, Second Edition
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Final and Abstract Classes
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Inner Classes 25-Oct-19.
Presentation transcript:

Nested class

Nested Class The Java programming language allows us to define a class within another class. Such a class is called a nested class. Example: class OuterClass { ... class NestedClass }

Types of Nested Classes A nested class is a member of its enclosing class. Nested classes are divided into two categories: static non-static Nested classes that are declared static are simply called static nested classes. Non-static nested classes are called inner classes.

Why Use Nested Classes? Logical grouping of classes—If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Increased encapsulation—Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world. More readable, maintainable code—Nesting small classes within top-level classes places the code closer to where it is used.

Static Nested Classes A static nested class is associated with its outer class similar to class methods and variables. A static nested class cannot refer directly to instance variables or methods defined in its enclosing class. It can use them only through an object reference. Static nested classes are accessed using the enclosing class name: OuterClass.StaticNestedClass For example, to create an object for the static nested class, use this syntax: OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();

Example

Example

Inner Classes (non-static nested classes) An inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Because an inner class is associated with an instance, it cannot define any static members itself. Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes: class OuterClass { ... class InnerClass { ... } }

Example

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance. To instantiate an inner class, we must first instantiate the outer class. Then, create the inner object within the outer object. Syntax: OuterClass.InnerClass innerObject = outerObject.new InnerClass();

Example (accessibility of nested classes in method)

Example (accessibility)

Example (deeply nesting classes)

Classification of Inner classes Additionally, there are two special kinds of inner classes: local classes and anonymous classes (also called anonymous inner classes).

Local Classes Local classes are classes that are defined in a block, which is a group of zero or more statements between balanced braces. For example, we can define a local class in a method body, a for loop, or an if clause. A local class has access to the members of its enclosing class. A local class has access to local variables. However, a local class can only access local variables that are declared final.

Example (creating and accessing local class)

Example (accessing local variables)

Example (accessing other local class)

Anonymous Class

Anonymous Classes Anonymous classes enable us to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. The anonymous class expression consists of the following: 1. The new operator 2. The name of an interface to implement or a class to extend. 3. Parentheses that contain the arguments to a constructor, just like a normal class instance creation expression. 4. A body, which is a class declaration body. More specifically, in the body, method declarations are allowed but statements are not.

Example A class is created but its name is decided by the compiler which extends the ProgrammerInterview class and provides the implementation of the read() method. An object of Anonymous class is created that is referred by pInstance reference variable of ProgrammerInterview type.

Anonymous inner class An anonymous inner class is not an independent inner class. It is a sub-class of either a class type or an anonymous implementer of the specified interface type. So, when anonymous inner classes are in picture, polymorphism must be there. And when polymorphism is there you can only call methods from parent class reference those are defined in the reference variable type. Java's anonymous inner classes being sub-classes or implementers do strictly adhere to the polymorphism rules.

Example (using class) “dog” is the reference variable of superclass Dog It is used to hold the instance (or object) of subclass which is anonymous. In other words, the Dog reference variable “dog” refers not to an instance of Dog but to an instance of an anonymous inner subclass of Dog. It overrides one or more methods of the super class on the fly.

Example (using interface)

Key points Anonymous classes have the same access to local variables of the enclosing scope as local classes: An anonymous class has access to the members of its enclosing class. An anonymous class cannot access local variables in its enclosing scope that are not effectively final. Anonymous classes also have the same restrictions as local classes with respect to their members: We cannot declare static initializers or member interfaces in an anonymous class. An anonymous class can have static members provided that they are constant variables. Note that we can declare the following in anonymous classes: Fields Extra methods (even if they do not implement any methods of the supertype) Local classes we cannot declare constructors in an anonymous class.

Example

Example (declaring, instantiating & passing anonymous class object)

Anonymous inner class Remember, anonymous inner classes are inherited ones, and we always use a superclass reference variable to refer to an anonymous subclass object. And, we can only call methods on an anonymous inner class object that are defined in the superclass. Though, we can introduce new methods in anonymous inner class, but we cannot access them through the reference variable of superclass because superclass does not know anything about new methods or data members introduced in subclass.