1 Object-Oriented Software Engineering CS288. 2 Multiple Classes Contents Defining multiple classes Using objects of one class in another Multiple constructor.

Slides:



Advertisements
Similar presentations
CSCI 160 Midterm Review Rasanjalee DM.
Advertisements

Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
CS 211 Inheritance AAA.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
UniS 1 Object-Oriented Software Engineering CS288.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
UniS 1 Object-Oriented Software Engineering CS288.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
1 Object-Oriented Software Engineering CS Multiple Classes Contents Lists of objects Vectors Growing and shrinking Vectors Iteration over Vector.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Introduction to Object-Oriented Programming
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
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.
Chapter 3 Object Interaction.  To construct interesting applications it is not enough to build individual objects  Objects must be combined so they.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
1 Object-Oriented Software Engineering CS Java OO Fundamentals Contents Classes and Objects Making new objects Method declarations Encapsulating.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Unit Testing Part 2: Drivers and Stubs
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
 2005 Pearson Education, Inc. All rights reserved. 1 Classes and Objects: A Deeper Look.
Methods.
Topics Instance variables, set and get methods Encapsulation
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
A Simple Object Oriented Program public class Simple { public static void main (String [] args) { System.out.println(“howdy”); } System.out is an object.
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.
Static data members Constructors and Destructors
Java Primer 1: Types, Classes and Operators
Exceptions, Interfaces & Generics
Indexer AKEEL AHMED.
Simple Classes in Java CSCI 392 Classes – Part 1.
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
TCSS 143, Autumn 2004 Lecture Notes
Inner Classes 11-May-19.
Barb Ericson Georgia Institute of Technology Oct 2005
Inner Classes 18-May-19.
Corresponds with Chapter 5
Inner Classes 25-Oct-19.
Presentation transcript:

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 METHOD Otherwise, exactly same syntax as SimpleClass

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 here objects 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 accessing methods of field that is itself an 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 here objects 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 of ob1.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 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 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 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 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 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 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 Class Definition 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 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 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 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 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 Summing Up This lecture discussed Using objects of one class within the main method of another. Creating an inner class. Discovering stack overflows.