Download presentation
Presentation is loading. Please wait.
1
UniS 1 Object-Oriented Software Engineering CS288
2
UniS 2 Multiple Classes Contents Defining multiple classes Using objects of one class in another Nested objects Inner classes Infinite nesting error
3
UniS 3 Back to first version of SimpleClass public class SimpleClass { private String uselessField; public SimpleClass (String newFieldVal) { setUselessField (newFieldVal); } public String getUselessField () { return uselessField; } public void setUselessField (String newUselessField) { uselessField = newUselessField; } public static void main (String[ ] args) { // Add Code Here }
4
UniS 4 The FooClass class public class FooClass { private String fieldOfFoo; public FooClass (String newField) { setFieldOfFoo(newField); } public String getFieldOfFoo ( ) { return fieldOfFoo; } public void setFieldOfFoo (String fieldOfFoo) { this.fieldOfFoo = fieldOfFoo; } Note: NO MAIN METHOD Otherwise, exactly same syntax as SimpleClass
5
UniS 5 Creating Objects of one Class in Another Class public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); FooClass ob2 = new FooClass("Vote Snooks"); String stVal = ob2.getFieldOfFoo (); ob1.setUselessField (stVal); } Edit main method of SimpleClass to create object of FooClass
6
UniS 6 Executing main method public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); FooClass ob2 = new FooClass("Vote Snooks"); String stVal = ob2.getFieldOfFoo (); ob1.setUselessField (stVal); } FooClass SimpleClass When execution gets to here objects have these values
7
UniS 7 Executing main method public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); FooClass ob2 = new FooClass("Vote Snooks"); String stVal = ob2.getFieldOfFoo (); ob1.setUselessField (stVal); } When execution finishes here objects have these values
8
UniS 8 Add FooClass field to SimpleClass Add to SimpleClass a new field of type FooClass. private FooClass myFoo = new FooClass("Read Books"); This field can be used in main method.
9
UniS 9 New main method for SimpleClass public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); String stVal = ob1.myFoo.getFieldOfFoo( ); ob1.setUselessField (stVal); } Note syntax for accessing methods of field that is itself an object.
10
UniS 10 Executing main method public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); String stVal = ob1.myFoo.getFieldOfFoo( ); ob1.setUselessField (stVal); } When execution gets to here objects have these values myFoo is nested inside ob1
11
UniS 11 Executing main method public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); String stVal = ob1.myFoo.getFieldOfFoo( ); ob1.setUselessField (stVal); } When execution finishes here objects have these values
12
UniS 12 Syntax for nested objects Think of ob1.myFoo.getFieldOfFoo( ); as (ob1.myfoo).getFieldOfFoo( ); Hence we could achieve the same effect with: FooClass tmpObj = ob1.myfoo; tmpObj. getFieldOfFoo( );
13
UniS 13 Modify main method and test Change main method to code below. Can see with debugger in NetBeans that ob1 is changed exactly as before. public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); // String stVal = ob1.myFoo.getFieldOfFoo( ); // ob1.setUselessField (stVal); FooClass fooVal = ob1.myFoo; String stVal = fooVal.getFieldOfFoo ( ); ob1.setUselessField (stVal); }
14
UniS 14 Infinite Nesting Error What will happen if we add a new SimpleClass field to the FooClass as shown below? public class FooClass { private String fieldOfFoo; private SimpleClass mySimpCls = new SimpleClass("Give it welly"); public FooClass (String newField) { setFieldOfFoo(newField); } public String getFieldOfFoo () { return fieldOfFoo; } public void setFieldOfFoo (String fieldOfFoo) { this.fieldOfFoo = fieldOfFoo; }
15
UniS 15 Runtime Stack Overflow running program gives: Exception in thread "main" java.lang.StackOverflowError at multipleclassexample.FooClass. (FooClass.java:7) at multipleclassexample.SimpleClass. (SimpleClass.java:6) at multipleclassexample.FooClass. (FooClass.java:7) at multipleclassexample.SimpleClass. (SimpleClass.java:6) at multipleclassexample.FooClass. (FooClass.java:7) at multipleclassexample.SimpleClass. (SimpleClass.java:6) at multipleclassexample.FooClass. (FooClass.java:7) at multipleclassexample.SimpleClass. (SimpleClass.java:6) at multipleclassexample.FooClass. (FooClass.java:7) at multipleclassexample.SimpleClass. (SimpleClass.java:6) at multipleclassexample.FooClass. (FooClass.java:7) at multipleclassexample.SimpleClass. (SimpleClass.java:6) at multipleclassexample.FooClass. (FooClass.java:7)............................................. This happens because compiler is trying to create infinitely many objects when we invoke the SimpleClass constructor.
16
UniS 16 public class SimpleClass { private String uselessField; public SimpleClass () { } public SimpleClass (String newFieldVal) { setUselessField (newFieldVal); } public String getUselessField () { return uselessField; } public void setUselessField (String newUselessField) { uselessField = newUselessField; } public class SimpleInnerClass { public String innerField; public SimpleInnerClass (String newFieldVal) { innerField = newFieldVal; } public static void main (String[ ] args) { // TODO code application logic here } Inner Classes Inner Class Definition lies within the body of SimpleClass
17
UniS 17 Declaration For Inner Class This variation on the SimpleClass main method is legal: public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass (); SimpleInnerClass ob2; } It is quite legitimate to declare a variable to be an object of type SimpleInnerClass but we must provide a method that allows us to construct a new object of this class. The intention is for inner classes to be used only within the class they are defined in. If we want to have full access to a class and its methods, then it should be defined as a seperate class.
18
UniS 18 Inner Class can not be directly accessed We can not use an inner class within the main method in the same way we could use the FooClass, which was external to SImpleClass. If we try (as shown below) it will result in compiler error. The error shown is somewhat misleading. The SimpleInnerClass constructor belongs to an object of class SimpleClass. But we have not provided any way to invoke the constructor from a SimpleClass object.
19
UniS 19 Adding Method to create Inner Class This method, when added to SimpleClass, can be used to create objects of type SimpleInnerClass given a String argument. public SimpleInnerClass createInnerClassObject (String newStr) { SimpleInnerClass nwObj = new SimpleInnerClass(newStr); return nwObj; } The main method can now use this to create objects of this type. public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass (); SimpleInnerClass ob2 = ob1.createInnerClassObject("OK, done"); /* and so on */ }
20
UniS 20 Inner Classes are Not Visible from other Classes public class SimpleClass { // Code for fields and methods public class SimpleInnerClass { public String innerField; public SimpleInnerClass (String newFieldVal) { innerField = newFieldVal; } } class DiffClass { public DiffClass () { SimpleInnerClass tempObj; } } Trying to declare variable of type SimpleInnerClass in any other class leads to compiler errors
21
UniS 21 Summing Up This lecture discussed Using objects of one class within the main method of another. Creating an inner class. Discovering stack overflows.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.