SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Advertisements

TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
AbstractClassesInterfacesPolymorphism1 Abstract Classes, Interfaces, Polymorphism Barb Ericson Georgia Tech April 2010.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Unit 051 Packages What is a Package? Why use Packages? Creating a Package Naming a Package Using Package Members Managing Source and Class Files Visibility.
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
Georgia Institute of Technology Introduction to Java part 2 Barb Ericson Georgia Institute of Technology May 2006.
Java Programming Review (Part I) Enterprise Systems Programming.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
CPSC1301 Computer Science 1 Chapter 11 Creating Classes part 1.
Georgia Institute of Technology Workshop for Programming And Systems Management Teachers Chapter 9 Source File Anatomy.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
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.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
Software Documentation Section 5.5 ALBING’s Section JIA’s Appendix B JIA’s.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
1 JAVA API & Packages Starring: Java Documentation Co-Starring: BlueJ IDE.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming1 Programming.
CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Georgia Institute of Technology Creating Classes part 4 Barb Ericson Georgia Institute of Technology May 2006.
CreatingClasses-SlideShow-part41 Creating Classes part 4 Barb Ericson Georgia Institute of Technology Dec 2009.
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.
More on Objects Mehdi Einali Advanced Programming in Java 1.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
 2005 Pearson Education, Inc. All rights reserved. 1 Classes and Objects: A Deeper Look.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
More Sophisticated Behavior
3 Introduction to Classes and Objects.
Introduction to Java part 2
Manipulating Pictures, Arrays, and Loops part 3
Chapter 3: Using Methods, Classes, and Objects
Introduction to javadoc
Introduction to Java part 2
Manipulating Pictures, Arrays, and Loops part 3
Barb Ericson Georgia Institute of Technology Oct 2005
Workshop for Programming And Systems Management Teachers
Manipulating Pictures, Arrays, and Loops
Introduction to javadoc
Barb Ericson Georgia Institute of Technology June 2005
Introduction to Java part 2
More on Creating Classes
Chapter 6 Objects and Classes
Barb Ericson Georgia Institute of Technology Oct 2005
More on Creating Classes part 3
Presentation transcript:

SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008

SourceAnatomy2 Learning Goals Understand at a conceptual and practical level the anatomy of a Java source file –Optional Comments –Optional Package Declaration –Import Statements as needed –Class Declarations Attributes Constructors Methods

SourceAnatomy3 Structure of a Java Source File Optional Package statement Optional Import statements Comments (like author(s)) Class definition { Attributes - fields Constructors - initializers Methods - behaviors Optional Main method }

SourceAnatomy4 Packages Packages allow you to organize your classes into a hierarchy. The package hierarchy is also used for the directory structure. –edu.gatech.ice.Class should be in edu/gatech/ice/ Package names for packages intended for release outside the organization are like the reverse of the internet domain names. –com.sun.java.swing –edu.gatech Every class belongs to a package. If none is specified then the unnamed package is used. java.lang

SourceAnatomy5 Common Packages java.lang - Basic classes, including strings and objects java.util - Utility classes including generic data structures java.awt - User interface and graphics javax.swing - Newer user interface classes java.applet - Support for applets java.net - Networking support (URLs, TCP, UDP) java.io - Input and output to files and streams

SourceAnatomy6 Package Statement The package statement has to be the first line of code in a source file, if it is specified. It can follow a comment. –package packageName; for example /** Copyright notice */ package edu.gatech.ice;

SourceAnatomy7 Import Statements If you wish to use classes from a different package than the declared package you can import the class using import java.awt.Color; // import just Color or import java.awt.*; // all classes in java.awt –doesn’t include the file like in C so there is no penalty to using the wildcard –doesn’t extend to children of that level. For example importing java.awt.* doesn’t import java.awt.event.*; Or you can use the full name in your code –java.awt.Color

SourceAnatomy8 How do you know what to import? If you use a class that is part of Java and you get a compiler error –Error: Undefined class Use the all classes list of the API to find the class and check the documentation for the package it is in

SourceAnatomy9 Import Exercise Change SimplePicture.java to specify all the classes that are being imported –Rather than using import java.awt.*; Import java.io.*; Comment out the import statements with a wildcard ‘*” –select lines-> Edit-> Comment Line(s) Compile and see what classes it says are undefined Find the classes in the API and add a fully qualified import statement for each –import java.awt.Color;

SourceAnatomy10 Comments There are three types of comments in Java –A multi-line comment /* here is a multi-line comment it ends here */ –A single line comment // comments out the rest of this line –A javadoc comment /** Method to return the name of the person name - adds an author text - adds a version name description - adds a parameter description - describes the return value of a method

SourceAnatomy11 Comment Exercise Open SimplePicture.java and read some of the comments –Which are Javadoc comments? –Which are single line comments? –Which are multi-line comments? Click on Tools and select Preview Javadoc for Current Document –Compare the html documentation to the original Javadoc comments

SourceAnatomy12 Class Definition [Class modifiers] class ClassName [extends ClassName] [implements Interface1,…] { attributes, constructors, methods } Visibility Class Modifiers –no modifier means the class is visible to all classes in the same package –public - visible to any class. A file can have only one public class. –private - visible only to classes in the same file

SourceAnatomy13 Class Definition Examples public class Person implements Comparable { attributes, methods, and inner classes } private class Helper extends Object { attributes, methods, and inner classes }

SourceAnatomy14 Optional Extends Clause The “extends Class” clause tells you which class is the parent class –The one the current class inherits from –The parent, super, or base class If there is no “extends Class” clause –The class inherits from Object In the package java.lang

SourceAnatomy15 Inheritance Exercise What is the parent class of each of these classes? –Picture.java –SimplePicture.java –Sound.java –Pixel.java Where is the show method defined that Picture objects know how to do? Where is the play method defined that Sound objects know how to do?

SourceAnatomy16 Optional Implements Clause The “implements interface1, interface2” lets the class specify the interfaces that the class promises to implement –Any methods defined in the interfaces must be defined in the class Or the code will not compile An interface is like a communications contract –How two classes will communicate –Without worrying what about what classes they are An interface name can be used as a type –For variable declarations

SourceAnatomy17 Comparable Interface What if you want to compare two objects? –To find out if one is less than another (< 0) –Or equal to the other (= 0) –Or greater than the other (> 0) How would you compare people? –playing cards? –shoppers? Each class needs to decide what this means –But we need to know what method to call int compareTo(Object o)

SourceAnatomy18 Interface Exercise What interface does SimplePicture.java implement? Find that interface –Defined in a file with the interface Name.java What methods are declared in the interface? Find the same methods in SimplePicture.java

SourceAnatomy19 Attributes/Fields Attributes are the data that each object knows about itself. –All object methods in the class have access to the object attributes Can use just the attribute name Can also use this.attribute name Can also use getAttribute() if you follow Java naming conventions –Class and object methods have access to the class attributes Declared with “static” keyword

SourceAnatomy20 Attribute Exercise Open SimplePicture.java What are the attributes (fields)? –Which are object fields? –Which are class (static) fields? Change the load method to use –setFileName(fileName) instead of this.fileName = fileName; –Compile –Test by creating a picture and using System.out.println(picture);

SourceAnatomy21 Declaring Attributes Attributes are declared as [visibility] type name [= value]; Visibility none specified - classes in the same package public - all classes protected - class, subclasses, and other classes in the same package private - only the class itself Type - the type can be one of the following primitive type object type: class, interface, or array

SourceAnatomy22 Visibility Exercise Open SimplePicture.java –Which fields have public visibility? Why are these public? –Which fields have private visibility? Why are these private?

SourceAnatomy23 Constructors Constructors are called when the new object is created. They initialize the new object. Person currPerson = new Person(“Fred Flintstone”); Constructors don’t have a return type and have the same name as the class. public Person ()// no argument constructor There can be many constructors but their parameter signatures must be different. public Person() {} public Person(String theName) { name = theName; } }

SourceAnatomy24 Advanced Constructor Information The compiler will add a no-argument (no parameters) constructor –All fields will have their default values Objects default to null Numbers default to zero Booleans default to false If you add any constructors –The compiler will no longer add any for you –You will need to write the no-argument constructor One constructor can invoke another –Use this(parameter List) as first line

SourceAnatomy25 Inheritance and Constructors If classes have private attributes –Meaning they can’t be directly accessed outside the class –How can you initialize inherited attributes? Use super(parameters) to call the parent constructor Must be the first line in a constructor If not present the compiler will add a super() as the first line in a constructor Means the parent class must have a no-argument constructor if you aren’t calling super(parameters) in your constructors

SourceAnatomy26 Constructor Exercise Open SimplePicture.java –How many constructors does it have? –Why do the constructors call super()? Open Picture.java –How many constructors does it have? What is different about the constructors? –How would the complier decide which one to use?

SourceAnatomy27 Methods Methods are the things that object of the class know how to do. Methods are declared as [visibility] returnType methodName (parameter list) { … [return x;] // if returnType is not void } Example public String getName() { return name;}

SourceAnatomy28 Method Visibility and Return Type Visibility –none specified - classes in the same package –public - all classes –protected - class, subclasses, and other classes in the same package –private - only the class itself Return Type - the return type can be one of the following –primitive type: int, float, double... –object type: class, interface, or array –void if no value is returned

SourceAnatomy29 Method Parameter List Can have no parameters –public String getName() Parameters are listed as type name –public void setName(String name) Separate parameters with commas –public void multiplyTwo(int x, int y) You can have several methods with the same name but different parameter signatures. –public void multiplyTwo(float x, float y) –public void multiplyTwo(int x, int y)

SourceAnatomy30 Main Method Each class can have a main method public static void main(String[] argv) {} You tell Java which class to start with java ClassName Execution starts with the main method –There are no objects when you begin so the main method is static (a class method) In the main method you create object(s) of the class –Then start the simulation

SourceAnatomy31 Add Main Method Exercise Add a main method to Picture.java to –Create a picture from a specified file –Negate the picture –Show the picture Execute by Tools->Run Document’s Main Method –Notice that the interactions pane shows java Picture

SourceAnatomy32 Summary The package statement is the first code in a class file. –package edu.gatech.ice; Import statements follow the package statement. –import edu.gatech.edu.*; There are three kinds of comments in Java –// comment or /* comment */ or /** comment */ The class definition follows the import statement. Inside a class definition you declare attributes, constructors, and methods.