Sadegh Aliakbary Sharif University of Technology Fall 2012.

Slides:



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

Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
DATA STRUCTURES Lecture: Initialization & Cleanup Slides adapted from Prof. Steven Roehrig.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
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.
CMSC 132 Week2 Lab1 Comparable vs Comparator clone() finalize()
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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.
Object Oriented Programming: Java Edition By: Samuel Robinson.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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!
Peyman Dodangeh Sharif University of Technology Fall 2013.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Session 7 Methods Strings Constructors this Inheritance.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Objects and Classes Mostafa Abdallah
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary Sharif University of Technology Fall 2012.
Finalizers, this reference and static Sangeetha Parthasarathy 06/13/2001.
More on Objects Mehdi Einali Advanced Programming in Java 1.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Peyman Dodangeh Sharif University of Technology Spring 2014.
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.
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
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.
Dr. Majed Abdouli © Objects and Classes 1 Dr. Majed Abdouli © 2015, adapted from Liang, Introduction to Java Programming, Eighth Edition, (c) 2011.
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.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Lecture 6 Object Oriented Programming Using Java
Static data members Constructors and Destructors
Object Initialization and Clean up
Intro To Classes Review
Advanced Programming in Java
Classes & Objects: Examples
Everything the light touches, Simba, will be yours
Advanced Programming Behnam Hatami Fall 2017.
Code Animation Examples
Sampath Kumar S Assistant Professor, SECE
Classes Lecture 7 from Chapter /1/11.
class PrintOnetoTen { public static void main(String args[]) {
Basics of OOP A class is the blueprint of an object.
Presentation transcript:

Sadegh Aliakbary Sharif University of Technology Fall 2012

Agenda Init methods Constructors No Destructor Initialization Cleanup Fall 2012Sharif University of Technology2

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 Fall 2012Sharif University of Technology3

public class Student { //Mandatory private String name; private long id; //Optional private String homepage;... } Fall 2012Sharif University of Technology4

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; } Fall 2012Sharif University of Technology5

Initialization Method public void init(String name, long id) { setName(name); setId(id); } Fall 2012Sharif University of Technology6

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()); } Fall 2012Sharif University of Technology7

Other Examples Circle c = new Circle(); c.init(12); Book b1 = new Book(); b1.init(“من او”, “رضا اميرخانی”); Book b2 = new Book(); b2.init(“شاهنامه”, “ابوالقاسم فردوسی”); Fall 2012Sharif University of Technology8

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 Fall 2012Sharif University of Technology9

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 Fall 2010Sharif University of Technology10

Constructor example Fall 2010Sharif University of Technology11

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. Fall 2012Sharif University of Technology12

Fall 2010Sharif University of Technology13 Default Constructor Constructor

Fall 2010Sharif University of Technology14

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? Fall 2012Sharif University of Technology15

Destructor Java needs no destructor Destructor method in C++ Java has a finalize() method You can implement it for your class Fall 2012Sharif University of Technology16

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 Fall 2010Sharif University of Technology17

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); } Fall 2012Sharif University of Technology18

Fall 2010Sharif University of Technology19