Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Similar documents
Java: Comment Text. Introduction. Concepts

Introduction to Programming Using Java (98-388)

And Even More and More C++ Fundamentals of Computer Science

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

7. C++ Class and Object

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Introduction to Java Applications

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Math Modeling in Java: An S-I Compartment Model

Lecture 18 Tao Wang 1

Preview from Notesale.co.uk Page 3 of 36

Java Bytecode (binary file)

Section 2.2 Your First Program in Java: Printing a Line of Text

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Chapter 6 Introduction to Defining Classes

Computer Programming, I. Laboratory Manual. Final Exam Solution

Chapter 10 Introduction to Classes

CS 101 Fall 2006 Midterm 3 Name: ID:

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Interview Questions of C++

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

GCC C++: Comment Text

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

AP CS Unit 3: Control Structures Notes

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

1. Download the JDK 6, from

Supplementary Test 1

Defining Classes and Methods

Packages Inner Classes

Computer Programming: C++

Fundamentals of Object Oriented Programming

Programming overview

4. Structure of a C++ program

Introduction To C#.NET

APCS Semester #1 Final Exam Practice Problems

Introduction to Computer Science I

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Creating Classes and Objects

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

class objects instances Fields Constructors Methods static

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Full file at

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

C++ : Object Oriented Features. What makes C++ Object Oriented

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

download instant at

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Certified Core Java Developer VS-1036

Programming - 2. Common Errors

CIS133J. Working with Numbers in Java

Object-Oriented Programming Concepts

Section 2.2 Your First Program in Java: Printing a Line of Text

C++ (classes) Hwansoo Han

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

2.8. Decision Making: Equality and Relational Operators

Pace University. Fundamental Concepts of CS121 1

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Computer Science is...

Simple Java Reference

BBM 102 Introduction to Programming II Spring Abstract Classes and Interfaces

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Objects and Classes Lecture 2

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

More C++ : Vectors, Classes, Inheritance, Templates

JAVA Programming Language Homework I - OO concept

Chapter 6 Lab Classes and Objects

C++ Quick Guide. Advertisements

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Introduction to Java

CS 162, Lecture 25: Exam II Review. 30 May 2018

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Class, Variable, Constructor, Object, Method Questions

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Programming with Java

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Introduction to Software Development (ISD) Week 3

Chapter 4 Defining Classes I

Do not start the test until instructed to do so!

Producing Shapes on Screen

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Getting started with Java

Object Oriented Design

Introduction to Computer Science I Spring 2010 Sample mid-term exam Answer key

JAVA: A Primer. By: Amrita Rajagopal

Course Outline. Introduction to java

Introduction to Java. Handout-1d. cs402 - Spring

CS111: PROGRAMMING LANGUAGE II

1 Shyam sir JAVA Notes

Transcription:

Java: Classes Introduction A class defines the abstract characteristics of a thing (object), including its attributes and what it can do. Every Java program is composed of at least one class. From a programming point of view a class can be described as a template from which direct instances are generated. In Java a class is a data structure that may contain members. A member of a class is a class element that defines an attribute or an operation. Each member can be viewed as a sub-object. When a class is declared, an optional member list declares the class members. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation. This article explains how to create a class with one member and how to instantiate it and provides a simple example of an instance being used. Simply speaking, a polygon can be described as a flat shape with three or more straight sides. The clas program defines a class representing a polygon. The definition of a polygon states that it has a number of sides, but does not specify how many sides there are. The shapes below are all polygons. These shapes are actual objects that fall within the more generic category of a polygon. In programming terms, if the polygon is a class, each of these shapes is an instance of the polygon class. To define any of the shapes properly it is necessary to declare how many sides the shape has, and the polygon class thus needs to provide an attribute to allow this declaration to be made. This attribute is provided as a class member. The clas program declares an instance of the polygon class, called triangle, and specifies the number of its sides as three.

Concepts class A class defines the abstract characteristics of a thing (object), including its characteristics and what it can do. From a programming point of view a class can be described as a template from which direct instances are generated. The clas program defines a class identified by polygon to represent a polygon. member When a class is defined, the definition includes its members, which describe its characteristics. The members of a class can include data members (constants and variables) and function members (which include methods) which are specific to the class. The definition of the polygon class includes one data member, sides, to specify the number of its sides. instance An instance of a class is an object that has been created from the class template. A class is only a definition until an instance of the class is created. In objectoriented programming, a class is a template definition of the members in a particular kind of object. Thus, an object is a specific instance of a class; it contains real values instead of variables. The clas program declares an instance of the polygon class, called triangle, and specifies the number of its sides as three. instantiation Creation of a new instance from a class template is known as instantiation. Instantiation is performed in the clas program using the new keyword. definition A definition is a self-contained block of code with a specific purpose. The clas program contains a definition for the polygon class and the clas class, both of which are self-contained blocks of code.

Source Code The source code listing is as follows: /* clas.java Class. environment: language Java platform console */ // polygon class polygon { int sides ; } // class containing entry point class clas { public static void main( String[] args ) { polygon triangle ; triangle = new polygon() ; triangle.sides = 3 ; } } System.out.print( "triangle has " ) ; System.out.print( triangle.sides ) ; System.out.println( " sides." ) ;

Compiling and Running 1. Save the source code listing into a file named clas.java. 2. Launch a Windows command prompt. 3. Navigate to the directory clas.java was saved in. 4. To compile the program type: > javac clas.java 5. To run the program type: > java clas

Code Explanation class polygon { int sides ; } A class can be described as a template for objects used by the program. Classes are declared using the class keyword. The format of a simple class definition is as follows: where: class identifier { members } class is the keyword indicating that a class is being declared. In the definition of the polygon class, as in all class definitions, the keyword is the word class. identifier is the identifier by which it will be possible to refer to the class. The identifier of the class defined here is polygon. members contains the members of the class, which describes its characteristics. The section of the class defined between braces {} consists of member declarations or definitions and is known as the member list. The only member of the polygon class is the sides member which defines how many sides the polygon has. The identifier of the class declared above is polygon. The only characteristic of the polygon class is that it contains a sides member.

A member of a class is a class element that defines an attribute or an operation. A class member can exist in a number of forms including a field (or data member) which consists of data contained within the object. The format of a simple field declaration is the same as a simple variable declaration, as follows: where: type identifier ; type is the type specifier of the field. The type specifier in the declaration of the sides field is int, which specifies a 32-bit integer. identifier is the identifier by which it will be possible to refer to the field. The identifier of the sides field is sides. ; closes the declaration and delimits it from the next statement. class clas {... } All statements in Java, including the sides field declaration, are closed with the semicolon character ;. If a program is executable one of the classes available must have a static main method that serves as the entry point. The main method is a member of the class. The clas class is included in the clas program to provide the main method, and contains one member - the main method.

public static void main( String[] args ) {... } The main method is the entry point and is called when the program starts. In the clas program, the main method is the only member of the clas class. Class members may be prefixed by modifiers - a list of keywords that modify the member. Other than the type specifier, the main method declaration contains two modifiers: public and static. The private, protected and public modifiers control accessibility to class members. The public modifier declares a member to be visible to everyone. The main method is declared as public because it must be visible to everyone in order to allow the program to be called. A class member that has been modified with the static keyword is accessible without requiring an instance of the containing class. What this means is that the member can be accessed without declaring any instances based on the class. All that is required is for the member to be declared, as in the example above, and it can be accessed directly. The main method is declared using the static modifier to provide a global mechanism for calling the program, since at the time of calling no instances of the clas class will have been declared. polygon triangle ; A class constitutes a user-defined type and instances of the class may be represented by variables. When declaring the variable, the class identifier is used as the type. This statement declares a variable of type polygon named triangle.

triangle = new polygon() ; An instance is an object based on a class. Creation of an instance from a class is called instantiation. Instantiation is the process of creating a specific object which is a member or instance of a class. It involves creating an actual instance of a declared class. In Java the new keyword is used to instantiate a class. Expression new polygon() instantiates the polygon class, i.e. it creates an instance of it. The statement above assigns the newly created instance to variable triangle. A declaration statement and instantiation statement are often combined into one, so the two statements polygon triangle ; triangle = new polygon() ; could be rewritten as polygon triangle = new polygon() ; triangle.sides = 3 ; Once a class has been instantiated and the instance has been assigned to a variable, the instance can be accessed via the variable and so can its members. This statement assigns literal value 3 to the sides member of the triangle variable which represents an instance of the polygon class.

Further Comments Members of Java classes can be categorised as follows: data member constant constant value associated with the class field variable of the class function member * method implements the computations and actions that can be performed by the class property event constructor destructor named characteristic associated with reading and writing that characteristic notification that can be generated by the class implements actions required to initialise instances of the class implements actions to be performed before instances of the class are permanently discarded nested type type that is local to the class * Members that can contain executable code are collectively known as the function members of the class. This article discusses the field data member. Discussion of other members includes concepts that are outwith the scope of this article.