Tonga Institute of Higher Education

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 6 Object Oriented Programming in Java Language Basics Objects.
Road Map Introduction to object oriented programming. Classes
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Introduction to Java Programming, 4E Y. Daniel Liang.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
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.
D.L. Patel Institute of management & Technology (M.C.A. College),”Vidyanagri” Name: Bhatt Nishant D. Subject: OOP (object oriented programming language)
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
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.
Working With Objects Tonga Institute of Higher Education.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Code Conventions Tonga Institute of Higher Education.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Working With Objects Tonga Institute of Higher Education.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Learners Support Publications Constructors and Destructors.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Constructors and Destructors
Classes (Part 1) Lecture 3
Static data members Constructors and Destructors
Programming with ANSI C ++
Unit-2 Objects and Classes
Chapter 10: Void Functions
More About Objects and Methods
University of Central Florida COP 3330 Object Oriented Programming
Chapter 3: Using Methods, Classes, and Objects
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Constructor Overloading
Object Based Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Tonga Institute of Higher Education
Tonga Institute of Higher Education
Unit-1 Introduction to Java
Object Oriented Programming in java
Constructors and Destructors
Tonga Institute of Higher Education
Tonga Institute of Higher Education
PreAP Computer Science Quiz Key
CLASSES, AND OBJECTS A FIRST LOOK
Overview of Java 6-Apr-19.
Chapter 6 Objects and Classes
Object Oriented Programming in java
Chapter 9 Objects and Classes Part 01
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Constructors & Destructors
Classes and Objects Object Creation
Presentation transcript:

Tonga Institute of Higher Education Creating Classes Tonga Institute of Higher Education

Introduction The main method contains the code that is started when the application is first executed. Until now, we have always used objects that use the main method. But, many objects do not need the main method. Any class that does not need to be started when the application is first executed does not need a main method.

Custom Classes and Objects Sometimes, the objects we have do not fit our needs. We can define custom objects. We can use custom objects just like normal objects

Drivers Any class that does not have the main method cannot start by itself Often, developers make tools so they can test their class. A driver is a tool used to begin a program They are useful when testing objects

Demonstration StudentDriver

Create Our First Class Our class will be a student Each student has a: School First Name Last Name Program Age Gender

Demonstration Student

How to Define a Class Special Access Specifier Keyword Class Name public class Customer { //Body of Class } Access Specifier Class Name Special Keyword

Access Specifiers Default – This can be used by: public class Customer { //Body of Class } Access Specifier Class Name Special Keyword Default – This can be used by: Code in classes in the same package Public – This can be used by everything: Code in classes in different packages

Special Keyword and Class Name public class Customer { //Body of Class } Access Specifier Class Name Special Keyword Keyword Must always be class Class Names Class names should be nouns One word The first letter is capitalized. The first letter of each internal word is capitalized. Keep your class names simple and descriptive. Example: Customer SalesOrder

Constructors Constructor – A method that is automatically executed when an object is created. This allows you to set initial values for the object. Many objects have multiple constructors. (They are overloaded) You can find a list of constructors in the Java Developer’s Kit documentation. Show Java Doc

Default Constructors All classes have a default constructor The default constructor is a constructor that accepts no parameters Automatically called when the object is created Default constructors go away when you define your own constructors

Creating Constructors Constructors are methods that have the same name as the class Constructors do not return anything but do not include the word void. To overload constructors Define multiple methods with the same name as the class with different parameters Same name as class Overloaded

FavoriteFoods and FavoriteFoodsDriver Demonstration FavoriteFoods and FavoriteFoodsDriver

Garbage Collection When we create objects, we use memory space. If we never destroy objects, our computer will run out of memory and crash. Fortunately, the Java Garbage collector automatically destroys data. It runs behind the scenes and destroys an object when it detects an object that is no longer used by the program.

Finalizers Finalizer – A method that is automatically executed when an object is destroyed. public void finalize() { //Body of method }