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
Object Oriented Programming with Java
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
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.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
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 2010.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
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[]
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
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.
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.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
10-Nov-15 Java Object Oriented Programming What is it?
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Programming in Java CSCI-2220 Object Oriented Programming.
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.
Objects and Classes Mostafa Abdallah
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
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.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
More on Objects Mehdi Einali Advanced Programming in Java 1.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Topics Instance variables, set and get methods Encapsulation
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Class Fundamentals BCIS 3680 Enterprise Programming.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Advanced Programming in Java
Advanced Programming in Java
CS 302 Week 11 Jim Williams, PhD.
Advanced Programming in Java
Overloading and Constructors
Chapter 9 Inheritance and Polymorphism
امکانات شیءگرا در جاوا OOP in Java, a Deeper Look
Java Programming Language
Classes, Objects and Methods
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
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 Package Package access Static this Method overloading toString equals 3JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Access Specifiers public Interface access private Don’t touch! 5JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Example 6JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Public classes You can declare a class which is not public The class is visible within the file There can be only one public class per file The name of the public class must exactly match the name of the file including capitalization It is possible to have a java file with no public class Is not typical 7JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Object Access or Class Access Access specifiers are declared for classes Not for objects When a member is declared as private It is not visible in methods outside this class It is visible by other objects of the same class 8JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

public class Access { private String name; public Access(String name) { this.name = name; } public void check(Access access){ access.name = name; } public static void main(String[] args) { Access a = new Access("Ali"); Access b = new Access("Taghi"); a.check(b); System.out.println(a.name); System.out.println(b.name); } 9 a can touch private property (name) of b Because a and b has the same class name is not private for b name is private for Access JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Package A package contains a group of classes Organized together under a single namespace Packages organize classes belonging to the same category or providing similar functionality 11JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Package (2) A package provides a unique namespace for the types it contains Classes in one package has the same folder Packages may contain other packages Hierarchy of packages 12JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Packages and Folders 13JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

14JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Some famous java packages java.util java.lang java.io 15JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Package Access Remember public and private access specifiers The default access has no keyword It is commonly referred to as package access friendly Other classes in the current package have access to that member To classes outside of this package, the member appears to be private 16JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Java Packages import keyword Class Qualified Name = package-name + class-name For example java.lang.String java.lang.Math double sqrt = Math.sqrt(123); java.util.Scanner java.awt.Event org.w3c.dom.events.Event 17JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Using packages 18JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

java.lang java.lang is implicitly imported 19JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

No import in Byte Code Unused imports have a trivial impact on the compiler But there are no imports in the byte code or at runtime Unused import have no impact in runtime But it is better to remove them Organize Imports 20JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Writing eBay for Cars class Peykan{...} public class Pride { int color; int price; boolean isHatchBack; int designYear; int length, width; } 22 Properties of objects Properties of class JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Static When you create a class You describe how objects of that class look And how they will behave You don’t actually get an object until you create one using new At that point storage is allocated and methods become available. 23JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Static (2) There are two situations in which this approach is not sufficient. 1. Class data or static property 2. Class method or static method 24JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Class Data You want to have only a single piece of storage for a particular field regardless of how many objects of that class are created or even if no objects are created Example: Pride.designYear Person.? Class data = static properties 25JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Class Methods if you need a method that isn’t associated with any particular object of this class. You need a method that you can call even if no objects are created Class methods = static methods 26JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Static properties static properties are shared among all the objects static properties are properties of classes Not objects Example: Integer.MAX_VALUE Player.NumberOfObjects 27JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Static methods Static methods can access only static properties Static methods are actually class operations Not object operations If a method uses only static fields, make it static! Static methods are accessible via classes double d = Double.parseDouble("12"); String s = String.valueOf(12); public static void main(String[] args) Static properties and methods are invoked on class name Are not invoked on objects 28JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Sample package human; public class Person { private String name; private int age; public static int MAX_AGE = 150; public void setAge(int age) { if(age<MAX_AGE) this.age = age; } public static int getMAX_AGE() { return MAX_AGE; //no access to age and name } 29JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Static Initialization Static properties are class data, not object data Constructors are created for initializing object data How to initialize class data? Two ways: Inline values Static block Static initialization is done when Class Loader loads the class 30JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Inline initialization public static int MAX_AGE = 150; private static double PI = 3.14; static String defaultName= theDefaultName(); private static String theDefaultName() { return "Ali Alavi"; } 31JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Static Block public class Person { public static int MAX_AGE ; private static double PI ; static String defaultName ; private static String theDefaultName() { return "Ali Alavi"; } static{ MAX_AGE = 150; PI = 3.14; String s = theDefaultName(); if(s != null) defaultName = theDefaultName(); } 32JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Initialization Block 33JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Order of initialization Once per class Static variable declaration Static block Once per object variable declaration Initialization block Constructor 34JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

public class Person { public static int MAX_AGE ; private static double PI = 3.14; static{ MAX_AGE = 150; } private String nation = "Iran"; private int age; private String name; { name = "Ali"; } public Person(){ age = 10; } public Person(int a, String n){ age = a; name = n; } 35 Static variables Object variables public static void main(String[] args) { Person p1 =new Person(); Person p2 =new Person(20, "Taghi"); } JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Method-property access How does getArea() know where radius is? 37JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Compiler solution A reference to the object is implicitly passed to methods circle.getArea() is converted (by compiler) to something like: Circle.getArea(circle) What if you want to access this circle object? Use this keyword this is available within non-static methods 38JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Sample Application of this 39JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

public class Book { private String name; private Person author; public void setName(String name) { this.name = name; } public void setAuthor(Person author) { this.author = author; } 40 Sample Application of this (2) JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Static and this What are static methods? Methods without implicit this Static methods are bound to classes, not objects 41JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Method Overloading Methods with the same name in the same class With different parameters Different Signatures 43JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

44JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

No Return type method overloading Why this is not permitted? 45JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

But this one is OK int f(){ … return 0; } void f(int a){ … } 46JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Type Casting You can change the type by type casting Casting in reference types and other objects is not so simple See it later 48JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Type Conversion Sometimes we want to convert an object to another type Type casting is not the solution here We should write some methods to convert the types 49JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Type Conversion (2) 50JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

toString 51 toString() is a special method You can write it for every class JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

52JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Checking equality in java 53JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

equals method 54JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Creating Your Own Equals() public class Person { private String nationalID; private String name; private String ; private int age; public boolean equals(Person other) { return nationalID.equals(other.nationalID); } 55 Person p1 = new Person(" ", "Ali Alavi"); Person p2 = new Person(" ", "Taghi Taghavi"); Person p3 = new Person(" ", "Taghi Taghavi"); System.out.println(p1.equals(p2)); System.out.println(p2.equals(p3)); JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Creating Your Own Equals() In fact, equals() is more complicated The parameter should be an Object Person as the parameter was incorrect More checks are required IDE Support for writing equals Use your IDE for getters, setters, constructors, … 56JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (nationalID == null) { if (other.nationalID != null) return false; } else if (!nationalID.equals(other.nationalID)) return false; return true; } 57JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

58JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source