Nested Classes OOP tirgul No. 7 2006. Nested Classes Java allows you to define Nested Classes public class EnclosingClass { public int _dataMember = 7;

Slides:



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

Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.
OOP in Java – Inner Classes Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Nested Classes Yoshi Modified from:
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner 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.
OOP in Java – Inner Classes Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Inner Classes Overview  Java Inner Classes: A Definition.  Overview of Nested Classes.  Top level Nested Classes.  Non-static Inner Classes.  Non-static.
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
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.
Inner Classes in Java. Overview Inner class – one class nested inside another – static (i.e. class) rarely if ever used – member (i.e. instance) – anonymous.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Chapter 8: Inner Classes. You're an OO programmer, so you know that for reuse and flexibility/extensibility you need to keep your classes specialized.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
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 Programming (OOP) Lecture No. 11.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
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.
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
Nested Classes CompSci 230 S Software Construction.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Topics Instance variables, set and get methods Encapsulation
Object Oriented Programming in Java Habib Rostami Lecture 10.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
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.
Today Enumerated Types - Summary. Start Nested Classes: Inner Classes, Local Classes, Anonymous Classes, Lambda Functions Next: Interfaces, Abstract Classes.
Inner Classes.
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
Classes (Part 1) Lecture 3
Examples of Classes & Objects
CompSci 230 S Programming Techniques
Nested class.
Object Based Programming
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Overloading and Overriding
Classes & Objects: Examples
Inner Classes 29-Nov-18.
Java Inner Classes.
Method of Classes Chapter 7, page 155 Lecture /4/6.
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Inner Classes 1-May-19.
Inner Classes 11-May-19.
Object Oriented Programming (OOP) Lecture No. 11
Inner Classes 18-May-19.
Inner Classes.
Corresponds with Chapter 5
ITE “A” GROUP 2 ENCAPSULATION.
CSG2H3 Object Oriented Programming
Inner Classes 25-Oct-19.
Presentation transcript:

Nested Classes OOP tirgul No

Nested Classes Java allows you to define Nested Classes public class EnclosingClass { public int _dataMember = 7; public static class NestedClass { // static class defined inside // the scope of another class // is called a nested class public int _innerDataMember = 8; }

Nested Classes public class Check { static public void main(String[] args) { EnclosingClass en = new EnclosingClass() ; EnclosingClass.NestedClass in=new EnclosingClass.NestedClass(); System.out.println(en._dataMember+” “+in._innerDataMember); }

Nested Classes (2) Static Inner class can be instantiated with no dependence on the existence of instances of the enclosing class. For that reason, It has no access to an instance of an enclosing class. Can be declared private.

Privileges As a member of its enclosing class, It has unlimited access to its enclosing class members, even if they are declared private. The enclosing class can also access the nested class members. Actually it is not special – The access specifiers restrict access to members for classes outside the top-level class

Privileges Example public class EnclosingClass { private int _dataMember = 7; public void createAndIncrese() { InnerClass in = new InnerClass(); in._innerDataMember++; // a member of the inner class } public static class NestedClass { private int _innerDataMember = 8; void createAndIncrease() { EnclosingClass en = new EnclosingClass() ; en._dataMember++ ;// a member of the enclosing } // class }

Inner Classes A nested class without the static modifier, is called an Inner Class. Every instance of an inner class is linked to the enclosing instance that created it. It can be created only inside enclosing class, and the inner class can access the members of the enclosing class.

Inner Classes(2) public class EnclosingClass { public int _dataMember = 7; InnerClass getInnerClass() {return new InnerClass();} // notice the inner class is not static public class InnerClass { public void printDataMemberOfInstatiatingClass() { // prints the data member of the class that // created the instance System.out.println(_dataMember); }

Inner Classes(3) Because an inner class is associated with an instance, it cannot define any static members itself. Trying to create an instance of class InnerClass will cause a compilation error. // BAD EXAMPLE – Will not compile EnclosingClass.InnerClass in = new EnclosingClass.InnerClass() ;

Inner Class(4) Using the following syntax, you can create an object of type EnclosingClass.InnerClass Of course, in this case InnerClass must be public EnclosingClass en = new EnclosingClass() ; EnclosingClass.InnerClass in = en.new InnerClass() ;

Inner Class(5) An inner class instance can access the enclosing class instance that created it. public class InnerClass { void func() { EnclosingClass en = EnclosingClass.this ; system.out.println(en._dataMember) ; }

Local Classes Java enables to define a class inside a method. This can be useful when we have short algorithms with a common interface.

Local Classes(2) public class AnyClass { void localClassDemo() { // a function class LocalClass { // a class inside a function definition void func() { System.out.println(“in Local class”); } LocalClass local = new LocalClass(); local.func() ; }

Local Classes(3) public class AnyClass { HasFunc localClassDemo() { class Mul implements HasFunc { public int func(int a, int b) { return a*b ;} } return new Mul() ; } Interface HasFunc { public int func(int a, int b) ; }

Local Classes(4) Local Class can access the enclosing class members, and the ONLY the function variables which are defined final.

Local Classes(4) Local Classes are good for : –Encapsulation : the enclosing class does not “know” the Local Class. –Very useful in Event-Driven Programming

Anonymous classes There is a shorter syntax for creating local classes. This is very common in GUI’s Should be very short (one or two lines)

Anonymous classes public class AnyClass { static HasFunc anonymousDemo() { HasFunc h= new HasFunc() { public int func(int a, int b) { return a*b ;} } return h; } Interface HasFunc { public int func(int a, int b) ; }