Footnote To Trees: Inner Classes

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

1.00 Lecture 11 Scope and Access. Variable Lifecycles Instance (or object) variables – Created when their containing object is created – Initialized to.
Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.
Nested Classes Yoshi Modified from:
1 Composition A whole-part relationship (e.g. Dog-Tail) Whole and part objects have same lifetime –Whole creates instance of part in its constructor In.
CS102--Object Oriented Programming Lecture 17: – Linked Lists Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
trees1 Binary Trees trees2 Basic terminology nodesFinite set of nodes (may be empty -- 0 nodes), which contain data rootFirst node in tree is called.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Data Structures.
Inner Classes. Lecture Objectives Learn about inner classes. Know the differences between static and non- static inner classes. Designing and using inner.
CS102--Object Oriented Programming Lecture 16: – Inner classes + review Copyright © 2008 Xiaoyan Li.
Inner Classes in Java. Overview Inner class – one class nested inside another – static (i.e. class) rarely if ever used – member (i.e. instance) – anonymous.
CMSC 202 Inner Classes. Aug 7, Simple Uses of Inner Classes Inner classes are classes defined within other classes  The class that includes the.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
SEEM3460 Tutorial Java Keyword – static. static Variables class XY { static int x; // class variable int y; // instance variable } XY xy1 = new XY();
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
Binary Search Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 19 © 2002 Addison Wesley.
Splay Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 22 © 2002 Addison Wesley.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
Swing Threading Nested Classes COMP 401 Fall 2014 Lecture 23 11/25/2014.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
(c) University of Washington20-1 CSC 143 Java Trees.
N ESTED C LASSES -1. T YPES OF N ESTED C LASSES & I NTERFACES top-level nested classes interfaces inner classes member local named anonymous.
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.
A Concrete Presentation on Abstract Classes and Methods, Interfaces, and Polymorphism CSC 202.
Definition and Application of Binary Trees
Tree Insert Animation.
CompSci 230 S Programming Techniques
Objective: Understand Concepts related to trees.
ITEC 2620M Introduction to Data Structures
Nested class.
Interfaces and Inner Classes
CSC 205 Java Programming II
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
General Trees & Binary Trees
Chapter 15 Event-Driven Programming and Animations
Inner Classes 29-Nov-18.
Binary Trees.
Data Structures and Algorithms for Information Processing
The const Keyword The answer is that, yes, we don’t want the function to change the parameter, but neither do we want to use up time and memory creating.
Chapter 16 Tree Implementations
Classes, Constructors, etc., in C++
Binary Trees CS-2851 Dr. Mark L. Hornick.
Inner Classes.
CMSC 202 Inner Classes.
Which best describes the relationship between classes and objects?
Section 3.4 Solving Equations with Distribution on Both Sides
CSC 143 Java Trees.
CSC 143 Java Linked Lists.
Inner Classes 17-Apr-19.
CSC 143 Binary Search Trees.
Inner Classes 21-Apr-19.
Chapter 20: Binary Trees.
Compound Conditionals
Inner Classes 1-May-19.
Inner Classes 11-May-19.
Podcast Ch27b Title: AVLTree implementation
Inner Classes 18-May-19.
Footnote To Trees: Inner Classes
Haidong Xue Summer 2011, at GSU
CSC 205 – Java Programming II
Binary Tree Implementation And Applications
Inner Classes 25-Oct-19.
Presentation transcript:

Footnote To Trees: Inner Classes CSC 143 Java Footnote To Trees: Inner Classes (c) 1997-2003 University of Washington

(c) 1997-2003 University of Washington A Programming Dilemma The nodes we've defined so far for linked lists and trees have been public classes with public instance variables: public class BTNode { public Object item; // data item in this node public BTNode left; // left subtree, or null if none public BTNode right; // right subtree, or null if none public BTNode(Object item, BTNode left, BTNode right) { … } } This simplifies examples, and increases performance... buts it's very bad practice. When one class (like a node) is used only as a helper to another class.. It would be ideal to keep it inaccessible to the outside, without giving up programming convenience or speed. (c) 1997-2003 University of Washington

Solution: Inner Classes One class may be defined fully within another class Called an "inner class" class OuterClass { //constructors, variables, methods... and: class InnerClass { //contructors, variables, methods of InnerClass ... } //end class Inner } //end class Outer Inner class can be marked public, protected, or private Just like instance variables and methods Containing class can always reference its own private instance variables, methods – and inner classes! (c) 1997-2003 University of Washington

Solving the Tree/Node Problem Make Node a private inner class of BinTree: public class BinTree { //constructors, variables, methods... and: private class BTNode { item; // data item in this node BTNode left; // left subtree, or null if none BTNode right; // right subtree, or null if none BTNode(Object item, BTNode left, BTNode right) { … } } //end class BTNode } //end class BinTree BinTree has full access to the members of BTNode Regardless of member public/protected/private marking (c) 1997-2003 University of Washington

More About Java Inner Classes We've been using inner classes occasionally without calling attention to it. Point2D.Double means: the (public) inner class named Double of the class named Point2D. The inner/outer relationship is not the same as inheritance or composition I.e., neither is-a or has-a Inner classes have many interesting twists and turns Inner classes can even be anonymous (unnamed), like objects (c) 1997-2003 University of Washington