Object Oriented Programming in java

Slides:



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

 Pearson Education, Inc. All rights reserved static Class Members static fields – Also known as class variables – Represents class-wide.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look - modified by Eileen Kraemer.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java programming Package. A package is a group of similar types of classes, interfaces and sub-packages. Package can be categorized in two form, built-
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
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.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
© 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.
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.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Object Oriented Programming
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Object-Based Programming in VB.NET. Must Understand Following: Encapsulation Information hiding Abstract Data Type Class, Instance, Reference Variable.
Classes, Interfaces and Packages
CSI 3125, Preliminaries, page 1 Packages. CSI 3125, Preliminaries, page 2 Packages Packages are containers for classes Collection of classes Packages.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Inheritance. Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object Inheritance represents the IS-A.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
CompSci 230 S Programming Techniques
Topic: Classes and Objects
Classes and Objects: A Deeper Look
Classes and Objects: A Deeper Look
Static data members Constructors and Destructors
Java Primer 1: Types, Classes and Operators
Classes and Objects: A Deeper Look (Chapter 8)
CSC240 Computer Science III
Object Based Programming
Chapter 6 Methods: A Deeper Look
Chapter 9 Classes: A Deeper Look, Part 1
Lecture 22 Inheritance Richard Gesick.
Chapter 9 Objects and Classes
Introduction to Classes and Objects
Sampath Kumar S Assistant Professor, SECE
S.VIGNESH Assistant Professor/CSE, SECE
JAVA Constructors.
6 Methods: A Deeper Look.
Object Oriented Programming in java
OO Programming Concepts
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
Chapter 8 Classes and Objects: A Deeper Look
Chapter 7 Objects and Classes
Presentation transcript:

Object Oriented Programming in java Dana F. Doghramachi Chapter -6- Classes and Objects: A Deeper Look 2017

6.1 Overloaded Constructors Overloaded constructors enable objects of a class to be initialized in different ways. To overload constructors, simply provide multiple constructor declarations with different signatures. Recall that the compiler differentiates signatures by the number of parameters, the types of the parameters and the order of the parameter types in each signature. The compiler invokes the appropriate constructor by matching the number, types and order of the types of the arguments specified in the constructor call with the number, types and order of the types of the parameters specified in each constructor declaration. Classes and Objects: A Deeper Look

6.1 Overloaded Constructors (Cont.) A program can declare a so-called no-argument constructor that is invoked without arguments. Such a constructor simply initializes the object as specified in the constructor’s body. Using this in method-call syntax as the first statement in a constructor’s body invokes another constructor of the same class. Popular way to reuse initialization code provided by another of the class’s constructors rather than defining similar code in the no- argument constructor’s body. Once you declare any constructors in a class, the compiler will not provide a default constructor. Classes and Objects: A Deeper Look

Classes and Objects: A Deeper Look Class Student { int id; String name; int age; String stage; Student(int i,String n){ //2 parameter constractor id=i; name = n; } Student(int i,String n,int a){//3 parameter constr this(125,"Ali",20,"Second"); //4 parameter constra id = i; name = n; age=a; } Fig. 6.1 | Student class with overloaded constructors. (Part 1 of 3.) Classes and Objects: A Deeper Look

Classes and Objects: A Deeper Look Student(int i,String n,int a,String s){//4 parameter id = i; name = n; age=a; stage=s; System.out.println("ID : "+id+"\nName : "+name+"\nAge : "+age+"\nStage : "+stage);} void display() //method display {System.out.println("ID : "+id+"\nName : "+name+"\nAge : "+age);} public static void main(String args[]){ Student s1 = new Student(124,"Sidra"); Student s2 = new Student(126,"Ahmad",21); Fig. 6.1 | Student class with overloaded constructors. (Part 2 of 3.) Classes and Objects: A Deeper Look

Classes and Objects: A Deeper Look s1.display(); s2.display(); }} ID : 125 Name : Ali Age : 20 Stage : Second ID : 124 Name : Sidra Age : 0 ID : 126 Name : Ahmad Age : 21 Fig. 6.1 | Student class with overloaded constructors. (Part 3 of 3.) Classes and Objects: A Deeper Look

6.2 Garbage Collection and Method finalize Every class in Java has the methods of class Object (package java.lang), one of which is the finalize method. Rarely used because it can cause performance problems. The JVM performs automatic garbage collection to reclaim the memory occupied by objects that are no longer used. When there are no more references to an object, the object is eligible to be collected. This typically occurs when the JVM executes its garbage collector. Classes and Objects: A Deeper Look

6.2 Garbage Collection and Method finalize So, memory leaks that are common in other languages like C and C++ (because memory is not automatically reclaimed in those languages) are less likely in Java, but some can still happen in subtle ways. Other types of resource leaks can occur. An application may open a file on disk to modify its contents. If it does not close the file, the application must terminate before any other application can use it. Classes and Objects: A Deeper Look

6.2 Garbage Collection and Method finalize The finalize method is called by the garbage collector to perform termination housekeeping on an object just before the garbage collector reclaims the object’s memory. Method finalize does not take parameters and has return type void. A problem with method finalize is that the garbage collector is not guaranteed to execute at a specified time. The garbage collector may never execute before a program terminates. Thus, it’s unclear if, or when, method finalize will be called. For this reason, most programmers should avoid method finalize. Classes and Objects: A Deeper Look

Classes and Objects: A Deeper Look Fig. 6.2 | static variable used to maintain a count of the number of Employee objects in memory. (Part 1 of 2.) Classes and Objects: A Deeper Look

Classes and Objects: A Deeper Look Fig. 6.2 | static variable used to maintain a count of the number of Employee objects in memory. (Part 2 of 2.) Classes and Objects: A Deeper Look

Classes and Objects: A Deeper Look Fig. 6.3 | static member demonstration. (Part 1 of 3.) Classes and Objects: A Deeper Look

Classes and Objects: A Deeper Look Fig. 6.3 | static member demonstration. (Part 2 of 3.) Classes and Objects: A Deeper Look

Classes and Objects: A Deeper Look Fig. 6.3 | static member demonstration. (Part 3 of 3.) Classes and Objects: A Deeper Look

6.3 static Class Members (Cont.) Objects become “eligible for garbage collection” when there are no more references to them in the program. Eventually, the garbage collector might reclaim the memory for these objects (or the operating system will reclaim the memory when the program terminates). The JVM does not guarantee when, or even whether, the garbage collector will execute. When the garbage collector does execute, it’s possible that no objects or only a subset of the eligible objects will be collected. Classes and Objects: A Deeper Look

Classes and Objects: A Deeper Look 6.4  static Import A static import declaration enables you to import the static members of a class or interface so you can access them via their unqualified names in your class—the class name and a dot (.) are not required to use an imported static member. Two forms One that imports a particular static member (which is known as single static import) One that imports all static members of a class (which is known as static import on demand) Classes and Objects: A Deeper Look

Classes and Objects: A Deeper Look 6.4  static Import (Cont.) The following syntax imports a particular static member: import static packageName.ClassName.staticMemberName; where packageName is the package of the class, ClassName is the name of the class and staticMemberName is the name of the static field or method. The following syntax imports all static members of a class: import static packageName.ClassName.*; * indicates that all static members of the specified class should be available for use in the classes declared in the file. static import declarations import only static class members. Regular import statements should be used to specify the classes used in a program. Classes and Objects: A Deeper Look

Classes and Objects: A Deeper Look Fig. 6.4 | Static import of Math class methods. Classes and Objects: A Deeper Look

6.5 final Instance Variables Keyword final specifies that a variable is not modifiable (i.e., it’s a constant) and any attempt to modify it is an error. private final int INCREMENT; Declares a final (constant) instance variable INCREMENT of type int. A final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. If a class provides multiple constructors, every one would be required to initialize each final variable. A final variable cannot be modified by assignment after it’s initialized. If a final variable is not initialized, a compilation error occurs. Classes and Objects: A Deeper Look

Classes and Objects: A Deeper Look 6.6 Creating Packages Each class in the Java API belongs to a package that contains a group of related classes. Packages are defined once, but can be imported into many programs. Packages help programmers manage the complexity of application components. Packages facilitate software reuse by enabling programs to import classes from other packages, rather than copying the classes into each program that uses them. Packages provide a convention for unique class names, which helps prevent class-name conflicts. Classes and Objects: A Deeper Look

6.6 Creating Packages (Cont.) The steps for creating a reusable class: Declare a public class; otherwise, it can be used only by other classes in the same package. Choose a unique package name and add a package declaration to the source-code file for the reusable class declaration. In each Java source-code file there can be only one package declaration, and it must precede all other declarations and statements. Classes and Objects: A Deeper Look

6.6 Creating Packages (Cont.) //save by A.java   package pack;   public class A{     public void msg() { System.out.println("Hello"); }}   //save by B.java   package mypack;   import pack.*;   class B{     public static void main(String args[]){      A obj = new A();      obj.msg();     }}   Fig. 6.5 | Creating packages. Classes and Objects: A Deeper Look

6.6 Creating Packages (Cont.) If you import package.classname then only declared class of this package will be accessible. //save by B.java   package mypack;   import pack.A;      class B{      public static void main(String args[]){       A obj = new A();       obj.msg();      } } Fig. 6.6 | Creating packages. Classes and Objects: A Deeper Look

6.6 Creating Packages (Cont.) When your program uses multiple classes from the same package, you can import those classes with a type-import- on-demand declaration. Example: import java.util.*; // import java.util classes uses an asterisk (*) at the end of the import declaration to inform the compiler that all public classes from the java.util package are available for use in the program. Only the classes from package java.util that are used in the program are loaded by the JVM. Classes and Objects: A Deeper Look

سوپاس For Listening