CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Applying OO Concepts Using Java. In this class, we will cover: Defining classes Defining methods Defining variables Encapsulation Class methods and variables.
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
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)
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
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.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
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.
 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.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Programming in Java CSCI-2220 Object Oriented Programming.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
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)
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 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
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.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
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
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
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.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
Static data members Constructors and Destructors
Final and Abstract Classes
Road Map Introduction to object oriented programming. Classes
Object Based Programming
Classes & Objects: Examples
Week 3 Object-based Programming: Classes and Objects
Object Oriented Programming in java
Java – Inheritance.
Applying OO Concepts Using Java
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Applying OO Concepts Using Java
Final and Abstract Classes
ITE “A” GROUP 2 ENCAPSULATION.
SPL – PS3 C++ Classes.
Presentation transcript:

CS170 ygao JAVA, C4Slide 1

CS170 ygao JAVA, C4Slide 2

CS170 ygao JAVA, C4Slide 3 Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally with the same scope of the accessibility in the memory Why encapsulation - Objects no longer need to be dependent upon any “outside” functions and data after the creation - Provide ADT (Abstract Data Types) - Easier in coding - Promote standards in OOP

CS170 ygao JAVA, C4Slide 4 Classes vs. Objects Class is a data structure that encapsulates data and functions. Data in a class are called member data, class variables, instance variables, or fields. Functions in a class are called member functions or methods. Constructors are special methods. Objects are the instance of a class. Creation of the objects and instantiation of the objects are all referring to memory allocation to hold the data and methods of the objects. In Java, all objects are created dynamically using new operator. For example: ClassName ObjName = new ClassName();

CS170 ygao JAVA, C4Slide 5 name /member data / methods

CS170 ygao JAVA, C4Slide 6 Note: Diagrams do not include methods, just for demo

CS170 ygao JAVA, C4Slide 7

CS170 ygao JAVA, C4Slide 8

CS170 ygao JAVA, C4Slide 9

CS170 ygao JAVA, C4Slide 10 Characteristics of Constructors A special method that has the same name of the class without return type, not even void. Constructor will be automatically called whenever an object is created. Like a method, constructor can be overloaded for the flexibility of the object creation. A super class constructor can be called by a sub class object as: super();

CS170 ygao JAVA, C4Slide 11

CS170 ygao JAVA, C4Slide 12

CS170 ygao JAVA, C4Slide 13 set methods In OOP, particularly in Java, set() means to set object’s member data set() usually has void return and takes argument(s) to set the member data set() also checks the validity of the data before it sets Use meaningful name for a set() method

CS170 ygao JAVA, C4Slide 14 get methods In OOP, particularly in Java, get() means to return one of object’s member data get() usually has a return type and takes no argument Use meaningful name for a get() method

CS170 ygao JAVA, C4Slide 15 Complete Example Analysis

CS170 ygao JAVA, C4Slide 16 Complete Example Analysis (continue)

CS170 ygao JAVA, C4Slide 17 Complete Example Analysis (continue)

CS170 ygao JAVA, C4Slide 18 Complete Example Analysis (continue)

CS170 ygao JAVA, C4Slide 19 Static Fields What is a static field? –a variable that is not associated with any instance of the class, therefore it’s a class-wide field/variable/data –a variable that can be used without creation of the object –a private static field can be only use within the current class –can be constant (final), so it’s value cannot be modified; must be initialized when declaring Why static fields? –keep tracking class-wide information –Convenient to use and save memory –Be careful data encapsulation in using static fields

CS170 ygao JAVA, C4Slide 20

CS170 ygao JAVA, C4Slide 21

CS170 ygao JAVA, C4Slide 22

CS170 ygao JAVA, C4Slide 23

CS170 ygao JAVA, C4Slide 24

CS170 ygao JAVA, C4Slide 25

CS170 ygao JAVA, C4Slide 26 Static Initialization Block what is a static initialization block? –a block of code in a class starting with keyword static –the block will be automatically executed when any method of the class is called first time why static initialization blocks? –used when a static object/field cannot be initialized by a single statement –ensure the object/filed is ready/available to the rest of the methods in the class starting at the point of any method in the class that is called

CS170 ygao JAVA, C4Slide 27

CS170 ygao JAVA, C4Slide 28 Simple Example of Static Block 1.public class TestStaticApp{ 2.private static int y;//declaring a static varaible 3.//private int x = 0; //illigal to delcare regular variable here in app 4.static { //static initialization block 5.y = 99;//initializing the static variable 6.} 7. public static void main(String[] args){ 8. System.out.println(“Output from main()"); 9. test(); // calling a method 10. } 11. public static void test() //must be static method in app 12. { 13. System.out.println("static field value in test():" + y); 14. } 15.}

CS170 ygao JAVA, C4Slide 29

CS170 ygao JAVA, C4Slide 30

CS170 ygao JAVA, C4Slide 31 Packages what is a package? –is used to organize the classes both API and programmer defined classes why package? –is able programs to import classes as API –provides convention for unique class names to avoid the name conflict

CS170 ygao JAVA, C4Slide 32

CS170 ygao JAVA, C4Slide 33

CS170 ygao JAVA, C4Slide 34

CS170 ygao JAVA, C4Slide 35

CS170 ygao JAVA, C4Slide 36

CS170 ygao JAVA, C4Slide 37 javadoc tool what is javadoc tool? –allows you to create documentation for your classes in the same way java API does –is a command in JDK that does the documentation why javadoc? –create professional-like documentation –Other programmers can use to learn about the fields, constructors, and methods in the classes

CS170 ygao JAVA, C4Slide 38

CS170 ygao JAVA, C4Slide 39

CS170 ygao JAVA, C4Slide 40

CS170 ygao JAVA, C4Slide 41