Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Road Map Introduction to object oriented programming. Classes
Access to Names Namespaces, Scopes, Access privileges.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Java Programming Review (Part I) Enterprise Systems Programming.
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]
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Classes CS 21a: Introduction to Computing I First Semester,
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
10-Nov-15 Java Object Oriented Programming What is it?
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
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)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Classes, Interfaces and Packages
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.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
Methods.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Topic: Classes and Objects
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Final and Abstract Classes
Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Chapter 9 Inheritance and Polymorphism
CSC 113 Tutorial QUIZ I.
Classes, Objects, and Methods
Java Programming Language
Object Oriented Programming in java
Namespaces, Scopes, Access privileges
Java Programming Language
Classes CS 21a: Introduction to Computing I
Final and Abstract Classes
Corresponds with Chapter 5
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Java Class Structure

Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable declarations* constructor* class (static) methods* instance methods* * order immaterial

Package Declaration package packageName; first statement in the.java file (excepting comments) package components separated by dot; e.g., package uwt.css142.examples; packaged class files must live in a parallel directory structure; e.g., C:\java\classes\uwt\css142\examples CLASSPATH environment variable must point to parent directory; e.g., set CLASSPATH=.;C:\java\classes

Import Statements import packageName.className; import packageName.*; OR use fully-qualified name of class java.lang.* is imported automatically classes in the same package as the class being written don’t need to be imported import java.awt.Font; import java.awt.*; java.awt.Font f = new java.awt.Font(...);

Class Declaration [VisibilityModifier] class ClassName [extends SuperClassName] [implements SomeInterface[, AnotherInterface]] { visibility: public, default a public class must live in a file of the same name, with a.java suffix –this implies only one public class per file a class extends java.lang.Object by default public class MyClass extends Applet implements ActionListener, ItemListener

Visibility Modifiers public - visible everywhere protected - visible in subclasses and within the package default - visible within the package (no keyword) private - visible within the class only

Variable Declarations [visibility][static][final]type name [= value]; visibility: public, protected, private, default kinds of variables –class (static) variables –instance variables –local variables (visibility modifiers do not apply) final keyword indicates that the initial value of the variable cannot be changed –Can only be assigned to once –naming convention: all uppercase, with underscores –e.g, final double METERS_PER_FOOT =.3048;

Class (static) Variables not associated with an instance of the class (i.e, an object) are declared within a class but not inside any method are declared with the static keyword only one copy exists, no matter how many objects accessible to all objects private static int count; private static int nextAcctNo; public class (static) constants: public static final int BOLD = 1; public static final int ITALIC = 2; public static final int PLAIN = 4; – accessed through the class name: Font.BOLD

Instance Variables Hold the instance data for objects are declared within a class but not inside any method do not have the static keyword each object has its own copy are available throughout the class are usually hidden private String text; // hidden data public void setText (String s) // setter public String getText() // getter

Local Variables are defined inside the body of a method, loop, or other statement block ({}), or in a parameter list, or in for statement have no visibility modifier known only within the code block where defined can shadow instance variables of same name public class BankAccount { private double balance; // instance variable public BankAccount (double initBalance) { double balance = initBalance; // error }...

Method Definition [visibility] [static] returnType methodName ([Type param1], [Type param2],...) the body of the method is delineated by { } parameters are only known within the method (local variables) a return type must be declared; if the method returns nothing, use void if not returning void, every path through the method must have a return statement which returns data of the specified type There is an implied return statement at the end of a method that returns void; return statements may be inserted at other points e.g., public static int computeSum (int a, int b) { return a + b; } to invoke the above method from within the same class: int sum = computeSum (2*count, 145); int ans = computeSum (computeSum(4,9), 145);

Instance Methods the default; no static keyword must be invoked on an instance of the class, i.e., an object purpose is to access instance data e.g., public class BankAccount { private double balance; public double deposit (double amount) { balance = balance + amount; return balance; } invoked via an object of that class: BankAccount myAcct = new BankAccount (1000); double bal = myAcct.deposit (500);

Class (static) Methods identified by the static keyword NOT associated with an object e.g., public class Utilities { public static double centToFahr(double cent) { return cent * 9 / ; } invoked via the class name: double f = Utilities.centToFahr (20.) all data must be hard-coded or passed as parameter (or static) cannot access instance data

Method Overloading Suppose you want methods to operate on different parameter types: –void printInt (int i) –void printDouble (double d) –void printString (String s) Another way: methods of the same name are distinguished by their parameter types –void print (int i) –void print (double d) –void print (String s) Same name, but no compiler error as long as parameters are different in number and/or type; (return type not part of signature) Often used in the java API; e.g, valueOf(), indexOf() in String class have several variations

Constructors Invoked by the new operator to create an object Definition similar to method syntax, except no static option and no return type Constructor name is same as that of class Constructors may take parameters May be more than one constructor with different parameters (overloaded) Should generally be public Supply only if needed to initialize instance data public class Student { public Student () {...} public Student(String name) {...} public Student(String name, String major) {...} public Student(String name, String major, double gpa, int credits) {...} }

Constructors (continued) With overloaded constructors, it is common to have one constructor do all the work; the other constructors simply call it with the required parameters this (…); when used in a constructor, invokes another constructor of the same class public class Student { public Student(String name, String major){ this (name, major, 0.0, 0) } public Student(String name, String major, double gpa, int credits){ this.name = name;... }

Constructors (cont.) Every class must have a constructor The first thing a constructor must do is invoke its superclass constructor super(); or another constructor of the same class this(); If the author does not supply a constructor, the compiler will create one that takes no arguments and just calls its superclass constructor If the author supplies any constructor, the compiler does not create the no-args constructor If the author’s constructor does not invoke the superclass constructor, the compiler inserts an invocation of the no-args superclass constructor; if the superclass doesn’t have such a constructor, the class won’t compile In this way, the compiler assures the chaining of constructor invocations, from superclass to subclass supply a constructor if there is data to pass, or if the superclass doesn’t have a no-args constructor