Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.

Slides:



Advertisements
Similar presentations
1 Classes and Objects in Java Parameter Passing, Delegation, Visibility Control, and Object Cleanup.
Advertisements

Object Oriented Programming with Java
Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
DATA STRUCTURES Lecture: Initialization & Cleanup Slides adapted from Prof. Steven Roehrig.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary Sharif University of Technology Fall 2012.
Rounding Out Classes The objectives of this chapter are: To discuss issues surrounding passing parameters to methods What is "this"? To introduce class.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
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.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
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.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
CMSC 132 Week2 Lab1 Comparable vs Comparator clone() finalize()
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
More Object Concepts Chapter 4.  Our class is made up of several students and each student has a name and test grades  How do we assign the variables.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Session 7 Methods Strings Constructors this Inheritance.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Objects and Classes Mostafa Abdallah
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Chapter 7 Objects and Classes. OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Object Initialization and Clean up Mehdi Einali Advanced Programming in Java 1.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Static data members Constructors and Destructors
Object Initialization and Clean up
Intro To Classes Review
Everything the light touches, Simba, will be yours
Advanced Programming Behnam Hatami Fall 2017.
Sampath Kumar S Assistant Professor, SECE
Classes Lecture 7 from Chapter /1/11.
Basics of OOP A class is the blueprint of an object.
Presentation transcript:

Sadegh Aliakbary

Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP is clearly noted as the source in the used case. JAVACUP shall not be liable for any errors in the content, or for any actions taken in reliance thereon. Please send your feedback to 2JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Agenda Init methods Constructors No Destructor Initialization Cleanup JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source3

Initialization An instantiated object, is not a ready object It may be and invalid object Person p = new Person(); p is an object without name, id and, … p is an invalid object It should be initialized JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source4

public class Student { //Mandatory private String name; private long id; //Optional private String homepage;... } JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source5

public void setName(String s) { if (s != null && !"".equals(s.trim()) && s.matches("[a-zA-Z ]+")) name = s; } public void setId(long idValue) { if (id > && id < ) id = idValue; } public void setHomepage(String addr) { homepage = addr; } JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source6

Initialization Method public void init(String name, long id) { setName(name); setId(id); } JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source7

Using the Object public static void main(String[] args) { Student st = new Student(); // st is an invalid object now st.init("Hossein Alizadeh", ); // st is initialized now. ready to be used System.out.println(st.getName()); System.out.println(st.getId()); } JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source8

Other Examples Circle c = new Circle(); c.init(12); Book b1 = new Book(); b1.init(“من او”, “رضا اميرخانی”); Book b2 = new Book(); b2.init(“شاهنامه”, “ابوالقاسم فردوسی”); JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source9

init() Method What are the disadvantages of init() method? Init method is invoked manually There is no guarantee for init invocation Before calling init method, the object has an invalid state JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source10

Constructors Constructor is a special method With the same name as the class Without any return type A constructor is called when an object is instantiated No invalid object JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source11

Constructor example JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source12

Default Constructor Constructors may have parameters Default constructor : no parameter Is implicitly implemented You can write your own default-constructor If you write any constructor, default implicit constructor is vanished. JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source13

JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source14 Default Constructor Constructor

JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source15

Constructors usually instantiate their properties public class Car { private Engine engine; private Tyre[] tyres; public Car() { engine = new Engine(); tyres = new Tyre[4]; for (int i = 0; i < tyres.length; i++) { tyres[i] = new Tyre(); } } } Who does destruct what constructors has built? JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source16

Destructor Java needs no destructor Destructor method in C++ Java has a finalize() method You can implement it for your class JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source17

Finalize method Java has no delete Java has no destructor Java has a special method: finalize finilize() is called when the object is garbage- collected If garbage collector is not invoked finalize() method is not called Why we may need finalize? Garbage collection is only about memory JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source18

public class Circle { private double radius; public Circle(double r) { radius = r; } public String toString() { return "Circle [radius=" + radius + "]"; } public void finalize() throws Throwable { System.out.println("Finalize: " + toString()); } public static void main(String[] args) { f(); System.gc(); } private static void f() { Circle c = new Circle(2); System.out.println(c); } JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source19

JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source20