Download presentation
Presentation is loading. Please wait.
1
Nested Classes OOP tirgul No. 7 2006
2
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; }
3
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); }
4
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.
5
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
6
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 }
7
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.
8
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); }
9
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() ;
10
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() ;
11
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) ; }
12
Local Classes Java enables to define a class inside a method. This can be useful when we have short algorithms with a common interface.
13
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() ; }
14
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) ; }
15
Local Classes(4) Local Class can access the enclosing class members, and the ONLY the function variables which are defined final.
16
Local Classes(4) Local Classes are good for : –Encapsulation : the enclosing class does not “know” the Local Class. –Very useful in Event-Driven Programming
17
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)
18
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) ; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.