Overloaded Constructors constructors can be overloaded, like other methods – i.e., a class can define several constructors all constructors must have the.

Slides:



Advertisements
Similar presentations
Static Methods Static methods are those methods that are not called on objects. In other words, they don’t have an implicit parameter. Random number generation.
Advertisements

Lecture 2: Object Oriented Programming I
Lecture 8: Objects and Classes, cont’d discuss hw2 assign h3 Review the main points of objects/classes This Pass-by-value A new example: our Date class.
Access to Names Namespaces, Scopes, Access privileges.
Classes we can write our own classes – to represent and define our objects e.g. class for Complex objects represents a complex number defines its properties.
Java Library Java provides a huge library or collection of useful programs A gold mine of well-tested code that can save you countless hours of development.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
Lecture 3. 2 Introduction Java is a true OO language -the underlying structure of all Java programs is classes. Everything must be encapsulated in a class.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
Applications in Java Towson University *Ref:
Chapter 4 Objects and Classes.
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.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
10-Nov-15 Java Object Oriented Programming What is it?
1.  At the end of this slide, student able to:  Object-Oriented Programming  Research on OOP features.  Do a code walkthrough to examine the implementation.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Objects and Classes Mostafa Abdallah
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Non-static classes 1.  a utility class has features (fields and methods) that are all static  all features belong to the class  therefore, you do not.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
CS116 COMPILER ERRORS George Koutsogiannakis 1. How to work with compiler Errors The Compiler provide error messages to help you debug your code. The.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Libraries and APIs Don’t reinvent the wheel. What’s an API? API – Application Programmers Interface –We want to use code someone else has written –API’s.
CSE 1030: Implementing Static Features Mark Shtern.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
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.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Methods main concepts – method call – object 's methods – actual parameters – method declaration – formal parameters – return value other concepts – method.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Lecture 12 Implementation Issues with Polymorphism.
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.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Inheritance.
Lecture 3: Introduction to Object and Classes
Creating and Using Objects, Exceptions, Strings
OOP: Encapsulation &Abstraction
Haidong Xue Summer 2011, at GSU
Methods Chapter 6.
Yanal Alahmad Java Workshop Yanal Alahmad
More About Objects and Methods
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Chapter 6 Methods: A Deeper Look
Java Programming Language
Interfaces.
Object Oriented Programming in java
Chapter 8 Objects and Classes
Object Oriented Programming in java
Chapter 8 Objects and Classes
Chapter 9 Objects and Classes Part 01
OO Programming Concepts
Visibilities and Static-ness
Chapter 7 Objects and Classes
Presentation transcript:

Overloaded Constructors constructors can be overloaded, like other methods – i.e., a class can define several constructors all constructors must have the same name – it's the name of the class overloaded constructors must have different parameter list if there are no constructors, Java inserts an "empty" one – e.g. Complex () {} but watch out: if there are constructors and all have parameters, then Java doesn't insert an "empty" one!

Calling Constructors within a Constructor a constructor can reuse what another constructor does – it can call another constructor to call it, use this(), not the constructor's name e.g. Complex() { this(0, 0); // other code } – will call Complex(double real, double imaginary – with real = 0 and imaginary = 0 constraint: – call to this() must be the first statement in the constructor's body – there can be only one call to this()

Static Fields and Methods what if a property doesn't belong to any specific object – typically such property belongs to all objects of a class it has the same value, e.g., a constant – it belongs to the entire category i.e. the class what if a behavior isn't logically associated with any object – typically such behavior belongs to a class – e.g. math functions, sin(x), log(x) we can use static for such fields and such methods

Static Fields a static field belongs to the class – has the same value for all objects all constants are static and final – static because they don't belong to any object and their value is the same for each object – final because once they have been initialized, they can't be assigned inside the class static fields are used normally – as other variables outside the class, static fields must be prefixed with the name of the class – e.g., Math.PI

Static Methods a static method belongs to the class – it isn't associated with any object or we can think of it as common to all objects of this class inside the class static methods are used normally – as other methods outside the class, static methods must be prefixed with the name of the class – e.g., Math.sin() the body of a static method is constrained: – can use only static fields and static methods (of the class) some utility classes have only static fields and methods – e.g. class Math in package java.lang static methods are often used to return a unique object – e.g., Mouse mouse = System.getMouse(); – then we can use their methods, e.g. mouse.getPoint();