Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
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.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Road Map Introduction to object oriented programming. Classes
Lecture 2 Classes and objects, Constructors, Arrays and vectors.
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
Lecture 4: Objects and Classes Classes Objects Data fields (instance variables) Methods –Instance methods –Static methods –Constructors But first…
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
CSC 212 Object-Oriented Programming and Java Part 1.
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.
1 Software Construction Lab 4 Classes and Objects in Java Basics of Classes in Java.
ECE122 Feb. 22, Any question on Vehicle sample code?
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Programming in Java CSCI-2220 Object Oriented Programming.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
1 Chapter 3 – Object-Based Programming 2 Initializing Class Objects: Constructors Class constructor is a specical method to initialise instance variables.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Classes - Intermediate
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
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.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
CS180 Recitation Chapter 7: Defining Your Own Classes II.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Features of JAVA PLATFORM INDEPENDENT LANGUAGE JAVA RUNTIME ENVIRONMENT (JRE) JAVA VIRTUAL MACHINE (JVM) JAVA APP BYTE CODE JAVA RUNTIME ENVIRONMENT.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
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.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
COP Topics Java Basics - How to write & call your own method - Math methods (static methods) - String methods (instance methods) - Errors Classes.
Defining Your Own Classes II
CPSC 233 Tutorial 12 March 4/5 th, TopHat Quiz int[] a = {0}; int[] b = {1}; a = b; What is the value of a[0] i) 0 ii) 1.
3 Introduction to Classes and Objects.
Java Classes.
Object Oriented Programming
Advanced Programming in Java
CS 302 Week 11 Jim Williams, PhD.
CSC 113 Tutorial QUIZ I.
Interface.
Java Programming Language
Classes & Objects: Examples
Encapsulation and Constructors
Everything the light touches, Simba, will be yours
Interfaces.
Object Oriented Programming in java
Unit-2 Objects and Classes
Assignment 7 User Defined Classes Part 2
S.VIGNESH Assistant Professor/CSE, SECE
JAVA Constructors.
class PrintOnetoTen { public static void main(String args[]) {
Basics of OOP A class is the blueprint of an object.
Object-Oriented Programming
Java Programming Language
Chapter 11 Inheritance and Polymorphism Part 1
OO Programming Concepts
Chapter 5 Classes.
Presentation transcript:

Recitation 8 User Defined Classes Part 2

Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4); One is called using the class name (Math) and the other using an object identifier (str) How are they different? pow() is a class method whereas charAt is an instance method

Class Methods A class method is one which is defined with the static keyword. A class (static) method – can be called directly from any other static method of the class – can be called using the class name from an instance method or from another class (if it is public) – cannot directly call an instance method of the same class -- must be called on an object of that class – cannot access instance data members – only through an object

Instance Methods An instance method is one which is defined without the static keyword. An object method – can be called directly from any other object method of the class – can be called on an object of that class from either a static method or a method of another class. – can call a class method using the name of the class.

Methods public class Student{ static int totalStudents = 0; public static int getTotalStudents() { return totalStudents; } public static void incrementTotalStudents() { totalStudents++; } public static void main(String args[]) { incrementTotalStudents(); System.out.println(getTotalStudents()); }

Incorrect Use of Class Methods public class Student{ static int totalStudents = 0; public static int getTotalStudents() { return totalStudents; } public static void incrementTotalStudents() { totalStudents++; } public void test1() { incrementTotalStudents(); getTotalStudents(); }

Correct use of Class Methods public class Student{ static int totalStudents = 0; public static int getTotalStudents() { return totalStudents; } public static void incrementTotalStudents() { totalStudents++; } public void test1() { Student.incrementTotalStudents(); System.out.println(Student.getTotalStudents()); }

Class Data Members As with methods, we can have both instance data members and class data members. Both are defined outside any method. Class data members are those with the static keyword. There is only one copy of each class data member -- shared by all objects of the class Each object has its own copy of all instance data members.

Incorrect use of Instance Data Members public class Student{ int studentID; public Student(int id) { studentID = id; } public static void main(String args[]) { Student s = new Student(0); System.out.println(studentID); }

Correct use of Instance Data Members public class Student{ int studentID; public Student(int id) { studentID = id; } public static void test1() { Student s = new Student(0); System.out.println(s.studentID); }

Null null is a special value. Its type is that of a reference to an object (of any class). We can set an object identifier to this value to show that it does not point to any object. – Student student1=null; A method that returns objects (of any class) can return a null value. Note that you will get a run-time error if you access a data member of call a method of a null reference -- null pointer exception.

Null reference errors public class Student{ int studentID; public static Student getStudent(int id) { Student s = new Student(); s.studentID = id; return null; } public static void main(String args[]) { Student s = getStudent(0); System.out.println(s.studentID); }

Correct way to handle Null public class Student{ int studentID; public static Student getStudent(int id) { Student s = new Student(); s.studentID = id; return null; } public static void main(String args[]) { Student s = getStudent(0); if(s != null) System.out.println(s.studentID); }

Overloading Java allows multiple methods to share the same name – as long as they have different method signatures (i.e., different types of arguments, in order) – Can overload methods and constructors

Overloading Constructor public class Student{ int studentID; String name; public Student(int id) { studentID = id; } public Student(String name) { this.name = name; }

Overloading Methods public class Student{ int studentID; String name; public static Student getStudent(int id) { Student s = new Student(); s.studentID = id; return s; } public static Student getStudent(String name) { Student s = new Student(); s.name = name; return s; }

Organizing Classes into a Package For a class, A, to use another class, B, – their byte code files must be located in the same directory, OR – Class B has to be organized as a package and A must import class B A package is a collection of java classes that can be imported. – E.g., javax.swing.*

Creating a Package Package School: package school; public class Teacher{ } package school; public class Student{ }