Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
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.
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.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Chapter 5 Introduction to Defining Classes
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
 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.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Learners Support Publications Constructors and Destructors.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
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.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Constructors and Destructors
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Corresponds with Chapter 7
Object Oriented Programming in java
Constructors and Destructors
Chapter 7 Objects and Classes
SPL – PS3 C++ Classes.
Presentation transcript:

Classes

Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the class name. Constructors cannot return a value and, hence, cannot specify a return type, not even void, in the constructor header Constructors can be overloaded - A class can have more than one constructor.

Constructor Use constructor to construct new instances new operator is used to create an object. – A constructor can only be called in conjunction with new operator. A constructor can take zero, one, or more parameters.

Constructor //Constructor Student() { roll=0; name="A"; age=0; batch="S4 BSc."; marks=new int[5]; } Student(String n,int a,int r) { roll=r; name=n; age=a; batch="S4 BSc."; marks=new int[5]; }

Implicit and Explicit parameter Implicit parameter is the object itself – this keyword holds it. Parameters within parentheses – explicit parameters Calling another constructor – The keyword this has another meaning. – this can be used to call another constructor of the same class form the first line of a constructor – e.g public Employee() { this(“Amrita”); //calls Employee(String name); } – The keyword this has another meaning. this can be used to call another constructor

Accessibility of members - Introducing Access Control Encapsulation provides another attribute: access control – restricting access. Through this we can control what parts of a program can access the members of a class. How a member can be accessed is determined by the access specifier that modifies the declaration. Java has a set of access specifers; some are related to inheritance and package,

Accessibility of members - Introducing Access Control Java’s access specifiers are:- – public – private – protected – default level (i.e. when none of the is used)

Accessibility of members - Introducing Access Control public – that member can be accessed by any other code. private – that member can only be accessed by other members of its class. (why main() is public – its called by the JVM) no access specifier:- by default the member is public within its own package, not accessed outside. Protected – during inheritance

Static members Certain members should only belong to the class, and not be part of any object created from the class. An example of such a situation is when a class wants to keep track of how many objects of the class have been created. Maintain a counter for this.

Static vs. non-static members

Table 1.1. Terminology for Class Members Instance Members These are instance variables and instance methods of an object. They can only be accessed or invoked through an object reference. Instance Variable A field that is allocated when the class is instantiated, that is, when an object) of the class is created. Also called non-static field. Instance Method A method that belongs to an instance of the class. Objects of the same class share its implementation. Static Members These are static variables and static methods of a class. They can be accessed or invoked either by using the class name or through an object reference. Static Variable A field that is allocated when the class is loaded. It belongs to the class and not to any object of the class. Also called static field and class variable. Static MethodA method which belongs to the class and not to any object of the class. Also called class method.

Garbage Collection After creation of object - What about destroying them? In Java, the destruction of objects takes place automatically. An object exists in the memory (heap), and it can be accessed only through variables that hold references to the object. Student std = new Student(“Amrita"); std = null; Java uses a procedure called garbage collection to reclaim memory occupied by objects that are no longer accessible to a program. It is the responsibility of the system, not the programmer, to keep track of which objects are "garbage". System.gc()

More on methods Method parameters:- actual parameters & formal parameters. Explicit & implicit parameters. b1.method(a,b) class B { ….. public method(int x,int y) { …. }

More on methods - 2 Parameter passing:- – Call by value vs call by reference. – When a method is called, the actual parameter values are computed and copied into formal parameter variables. Accessors & Mutators Static methods. (main() – structured approach) Return statement

Structured approach in Java main() method calls another method. Since main() is static it can only access static methods. Declare any static method in the public class and it can be called from main()

Initialization Blocks Three ways to initialize a data field: - – Setting a value in a constructor – Assigning a value in the declaration – Initialization blocks Class declaration can contain arbitrary blocks of code. These blocks are executed whenever an object of the class is constructed. The initialization block is executed first and then the body of the constructor is executed.

Member Initialization In summary:- – All data fields are initialized to their default value (0,false,null). – All field initializers and the initialization blocks are executed, in the order in which they occur in the class declaration – If the first line of the constructor calls a second constructor, then the body of the second constructor is executed. – The body of the constructor is executed.