1 Object-Oriented Software Engineering CS288. 2 Multiple Classes Contents Defining multiple classes...

26
1 Object-Oriented Software Engineering CS288
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    0

Transcript of 1 Object-Oriented Software Engineering CS288. 2 Multiple Classes Contents Defining multiple classes...

1

Object-Oriented Software Engineering

CS288

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

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

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 METHODOtherwise, exactly same syntax asSimpleClass

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

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

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 hereobjects have these values

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

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

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 accessingmethods of field that is itselfan object.

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 hereobjects have these values

myFoo is nested inside ob1

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

Syntax for nested objects

Think ofob1.myFoo.getFieldOfFoo( );

as(ob1.myfoo).getFieldOfFoo( );

Hence we could achieve the same effect with:

FooClass tmpObj = ob1.myfoo;tmpObj. getFieldOfFoo( );

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

Infinite Nesting Error

What will happen if we add a new SimpleClass field to the FooClass as shownbelow?

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

Runtime Stack Overflow

running program gives:Exception in thread "main" java.lang.StackOverflowError at multipleclassexample.FooClass.<init>(FooClass.java:7) at multipleclassexample.SimpleClass.<init>(SimpleClass.java:6) at multipleclassexample.FooClass.<init>(FooClass.java:7) at multipleclassexample.SimpleClass.<init>(SimpleClass.java:6) at multipleclassexample.FooClass.<init>(FooClass.java:7) at multipleclassexample.SimpleClass.<init>(SimpleClass.java:6) at multipleclassexample.FooClass.<init>(FooClass.java:7) at multipleclassexample.SimpleClass.<init>(SimpleClass.java:6) at multipleclassexample.FooClass.<init>(FooClass.java:7) at multipleclassexample.SimpleClass.<init>(SimpleClass.java:6) at multipleclassexample.FooClass.<init>(FooClass.java:7) at multipleclassexample.SimpleClass.<init>(SimpleClass.java:6) at multipleclassexample.FooClass.<init>(FooClass.java:7) .............................................

This happens because compiler is trying to create infinitely many objects when we invokethe SimpleClass constructor.

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 objectof type FooClass that specify some or no initial values.

Java permits a class to have multiple constructor methods thatdo this job.

18

Multiple Constructor MethodsHere are three constructors for FooClass, they are all valid when addedto 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

Multiple Constructor Methodsclass 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

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

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 ClassDefinition lies within the body of SimpleClass

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 allowsus 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 aclass and its methods, then it should be defined as a separateclass.

23

Inner Class can not be directly accessed

We can not use an inner class within the main method in thesame way we could use the FooClass, which was external toSImpleClass.

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 classSimpleClass. But we have not provided any way to invokethe constructor from a SimpleClass object.

24

Adding Method to create Inner Class

This method, when added to SimpleClass, can be used to create objectsof 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

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 typeSimpleInnerClass in any other classleads to compiler errors

26

Summing Up

This lecture discussed

• Using objects of one class within the mainmethod of another.

• Creating an inner class.

• Discovering stack overflows.