Download presentation
Presentation is loading. Please wait.
1
1 Object-Oriented Software Engineering CS288
2
2 Multiple Classes Contents Defining multiple classes Using objects of one class in another Multiple constructor methods Nested objects Inner classes Infinite nesting error, stack overflow
3
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
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
5 The FooClass class The SimpleClass code exists as a file SimpleClass.java. The FooClass code exists as a file FooClass.java. NetBeans conveniently keeps track that they belong in the same project for us. For simple projects we only need to make sure the files live in the same folder for them to be in the same project automatically. Each class in the same project is able to access fields and methods of the other class where it has permission.
6
6 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
7
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); } FooClass SimpleClass When execution gets to here objects have these values
8
8 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
9
9 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.
10
10 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.
11
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 gets to here objects have these values myFoo is nested inside ob1
12
12 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
13
13 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( );
14
14 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); }
15
15 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; }
16
16 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.
17
17 Multiple Constructor Methods The constructor method defines how an object is to be initialised. That is it defines field values for the object at the point it is created The FooClass has three fields: private String fieldOfFoo; private Integer int_field; private Boolean bool_field; It may be that we want to have different ways to create an object of type FooClass that specify some or no initial values. Java permits a class to have multiple constructor methods that do this job.
18
18 Multiple Constructor Methods Here are three constructors for FooClass, they are all valid when added to the FooClass.java file at the same time. public FooClass (String newField) { setFieldOfFoo(newField); int_field = 12; bool_field = false; } public FooClass (String newField, Integer int_field) { setFieldOfFoo(newField); this.int_field = int_field; bool_field = false; } public FooClass (String newField, Integer int_field, boolean bool_field) { setFieldOfFoo(newField); this.int_field = int_field; this.bool_field = bool_field; }
19
19 Multiple Constructor Methods class FooClass { /* field declarations here */ public FooClass (String newField) { setFieldOfFoo(newField); int_field = 12; bool_field = false; } public FooClass (String newField, Integer int_field) { setFieldOfFoo(newField); this.int_field = int_field; bool_field = false; } public FooClass (String newField, Integer int_field, boolean bool_field) { setFieldOfFoo(newField); this.int_field = int_field; this.bool_field = bool_field; } /* and so on */ }
20
20 Multiple Constructor Methods For example, within the main method we could write: FooClass fc1 = new FooClass("Give me strength"); FooClass fc2 = new FooClass("Give me strength", 10); FooClass fc3 = new FooClass("Give me strength", 7, true);
21
21 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
22
22 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 separate class.
23
23 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.
24
24 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 */ }
25
25 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
26
26 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.