J AVA P ROGRAMMING 2 CH 04: C LASSES, O BJECTS AND M ETHODS (II) 0.

Slides:



Advertisements
Similar presentations
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
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
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Introduction to Object- Oriented Programming with Java Spring Semester 2003 Paul Krause.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Unit 061 Java Virtual Machine (JVM) What is Java Virtual Machine? The Class Loader Subsystem Linking oVerification oPreparation oResolution Class Initialization.
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
L EC. 04: C LASSES, O BJECTS AND M ETHODS 0. C ONTENT  Fundamentals of Java classes  Reference variables  Creating objects  Fundamentals of methods.
Programming Languages and Paradigms Object-Oriented Programming.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
1 Software Construction Lab 3 Classes and Objects in Java Basics of Classes in Java.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
J AVA P ROGRAMMING 2 C H 04: C LASSES, O BJECTS AND M ETHODS (I) 0 Java Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Classes and Methods. Classes Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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.
Programming Languages and Paradigms Activation Records in Java.
Methods.
Topics Instance variables, set and get methods Encapsulation
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
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
RealTimeSystems Lab Jong-Koo, Lim
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
2015 Spring Content Class method [review] Access control
Topic: Classes and Objects
Chapter 7 User-Defined Methods.
Static data members Constructors and Destructors
Chapter 11 Inheritance and Polymorphism
Topic: Classes and Objects TALENTSPRINT | © Copyright 2012
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Yanal Alahmad Java Workshop Yanal Alahmad
Object Oriented Programming
Advanced Programming in Java
EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun
Chapter 9 Inheritance and Polymorphism
An Introduction to Java – Part II
Stack Memory 2 (also called Call Stack)
Classes & Objects: Examples
Classes, Encapsulation, Methods and Constructors (Continued)
Unit-2 Objects and Classes
JAVA Constructors.
Constructors, GUI’s(Using Swing) and ActionListner
Object Oriented Programming in java
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Classes, Objects and Methods
ITE “A” GROUP 2 ENCAPSULATION.
Presentation transcript:

J AVA P ROGRAMMING 2 CH 04: C LASSES, O BJECTS AND M ETHODS (II) 0

C ONTENT  Fundamentals of Java classes  Reference variables  Creating objects  Fundamentals of methods  Class and instance methods  Parameters and arguments  Using the keyword this  Constructors  Fields  Comparisons among variables categories  Loading classes 1

C ONSTRUCTORS  A constructor of a class is syntactically similar to a method, but with no explicit return type, which initializes an object or performs any other startup procedures required to create a fully formed object when it is created.  Each class has at least one constructor.  If there is no constructor specified in a class, javac automatically adds a default constructor ( 預設建構子 ). A default constructor is a non-arg, i.e. without parameter, constructor and does nothing, except calls the non-arg constructor of its super class.  If there is a constructor defined, no default constructor will be added by javac. Fall Java Programming

C ONSTRUCTORS  The name of a class constructor must be the same as the class name.  A class may have more than one constructor, but with different parameter list.  Constructors are only activated by the new operator. Fall Java Programming

E XAMPLE OF USING CONSTRUCTORS class MyClass { int x, y; MyClass() { x = 0; y = 0; } MyClass(int a) { x = a; y = 0; } MyClass(int a, int b) { x = a; y = b; } } class DemoMyClass { public static void main(String args[]) { MyClass obj1 = new MyClass(); MyClass obj2 = new MyClass(5); MyClass obj3 = new MyClass(4, 3 + 4); DemoMyClass obj4 = new DemoMyClass(); } Fall Java Programming Remember javac automatically adds a default constructor for DemoMyClass class.

this KEYWORD  When a method is called, it is automatically passed an implicit argument that is a reference to the object receiving a message (that is, the object on which the method is called).  This reference is named this. Fall Java Programming

E XAMPLE OF USING this class Pwr { double base; int exp; double val; Pwr(double base, int exp) { // instance fields base and exp are // hidden by parameters with the same name this.base = base; // this.base refers to the instance field // base refers to the parameter this.exp = exp; this.val = 1; // both this.val and val refer to the instance field if(exp==0) return; for( ; exp>0; exp--) this.val = this.val * base; } double get_pwr() { return this.val; } Fall Java Programming

C OMPARISON VARIABLE CATEGORIES Fall Java Programming Analysis FactorFieldsParametersLocal variables UsageStoring class or object properties Passing input values to methods Temporarily storing computation results Where declaredInside a class definition, but outside a method definition In the method headerInside a method definition or a code block How declaredtype name [ = value];type name;type name [ = value]; How initializedDefault, explicit, or by a constructor By the caller during invocation Explicit initialization, NO DEFAULT ScopeWithin class or outside the class Within the methodWithin the method or specific statement block

L OADING CLASSES  When a class is referred, as an argument of the command java or used by executing a class, the class is loaded into JVM memory space by the class loaders.  JVM creates a java.lang.Class object to represent the loaded class.  The java.lang.Class class provides many services to process classes.  The reflection mechanism can be done by using these services provided by Class class.  Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. Fall Java Programming

E XAMPLE OF USING java.lang.Class class DemoClass { public static void main(String args[]) { DemoClass obj = new DemoClass(); Class classObj = obj.getClass(); // get the Class object // representing the class from // which obj is created System.out.println(“The class name of obj: “ + obj.getName()); } Fall Java Programming