9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Reusable Classes.  Motivation: Write less code!
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Chapter 11 Classes Continued
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter 10 Classes Continued
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Writing Classes (Chapter 4)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance (Part 4) Abstract Classes 1.  sometimes you will find that you want the API for a base class to have a method that the base class cannot.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Classes, Interfaces and Packages
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Web Design & Development Lecture 9
Lesson 10: Classes Continued
Sections Inheritance and Abstract Classes
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
9.1 Class (static) Variables and Methods
Software Development Java Classes and Methods
Inheritance and Polymorphism
Section 11.1 Class Variables and Methods
Java Programming Language
IFS410: Advanced Analysis and Design
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Java Inheritance.
Java Programming Language
Presentation transcript:

9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming comes from its capacity to reduce code and to distribute responsibilities for such things as error handling in a software system.

9.1 CLASS (STATIC) VARIABLES AND METHODS Interfaces. A Java interface specifies the set of methods available to clients of a class. Interfaces thus provide the glue that holds a set of cooperating classes together. public interface Shape{ public double area(); public void draw(Pen p); public double getXPos(); public double getYPos(); }

9.1 CLASS (STATIC) VARIABLES AND METHODS Inheritance. Java organizes classes in a hierarchy. Classes inherit the instance variables and methods of the classes above them in the hierarchy. A class can extend its inherited characteristics by adding instance variables and methods and by overriding inherited methods. Inheritance provides a mechanism for reusing code

9.1 CLASS (STATIC) VARIABLES AND METHODS Abstract Classes. Some classes in a hierarchy must never be instantiated, called abstract classes. Their sole purpose is to define features and behavior common to their subclasses. Pet Cat Dog Snake

9.1 CLASS (STATIC) VARIABLES AND METHODS Polymorphism. Polymorphism is when methods in different classes with a similar function are given the same name. Polymorphism makes classes easier to use because programmers need to memorize fewer method names. Ex) toString() method System.out.print(somebook.toString()); System.out.print(somePatron.toString());

9.1 CLASS (STATIC) VARIABLES AND METHODS Preconditions and Postconditions. Preconditions specify the correct use of a method Postconditions describe what will result if the preconditions are satisfied. Exceptions for Error Handling. When a method's preconditions are violated, a foolproof way to catch the errors is to throw exceptions, thereby halting program execution at the point of the errors Reference Types. The identity of an object and the fact that there can be multiple references to the same object are issues that arise when comparing two objects for equality and when copying an object.

9.1 CLASS (STATIC) VARIABLES AND METHODS Static Variables and Methods. Use when information needs to be shared among all instances of a class The word “static” must be used during method and variable declaration

9.1 CLASS (STATIC) VARIABLES AND METHODS A class variable belongs to a class. Storage is allocated at program startup and is independent of the number of instances created. A class method is activated when a message is sent to the class rather than to an object.

9.1 CLASS (STATIC) VARIABLES AND METHODS Counting the Number of Students Instantiated Suppose we want to count all the student objects instantiated during the execution of an application. We introduce a variable, which we call studentCount. This variable is incremented in the constructor every time a student object is instantiated. Because the variable is independent of any particular student object, it must be a class variable.

9.1 CLASS (STATIC) VARIABLES AND METHODS Counting the Number of Students Instantiated In addition, we need two methods to manipulate the studentCount variable: one to initialize the variable to 0 at the beginning of the application and the other to return the variable's value on demand. These methods are called setStudentCount and getStudentCount, respectively. Because these methods do not manipulate any particular student object, they must be class methods.

9.1 CLASS (STATIC) VARIABLES AND METHODS Modifying the Student Class Following are the modifications needed to add the class variable and the two class methods in the Student class. public class Student { private String name;... rest of the instance variables go here... public Student(){ studentCount++; // Increment the count when a student is // instantiated name = ""; test1 = 0; test2 = 0; test3 = 0; }

9.1 CLASS (STATIC) VARIABLES AND METHODS public void setName (String nm){ name = nm; }... rest of the methods without change go here... // class variables and methods static private int studentCount = 0; static public void setStudentCount(int count){ studentCount = count; } static public int getStudentCount(){ return studentCount; }

9.1 CLASS (STATIC) VARIABLES AND METHODS Here is some code that illustrates the new capabilities of the Student class:... Student.setStudentCount (0); // Initialize count to 0 s1 = new Student(); // Instantiate a student... s2 = new Student(); // Instantiate a student... s3 = new Student(); // Instantiate a student System.out.print(Student.getStudentCount()); // Displays 3

9.1 CLASS (STATIC) VARIABLES AND METHODS Notice that class messages are sent to a class and not to an object. Also, notice that we do not attempt to manipulate the studentCount variable directly because, in accordance with the good programming practice of information hiding, we declared the variable to be private. In general, we use a static variable in any situation where all instances share a common data value. We then use static methods to provide public access to these data.

9.1 CLASS (STATIC) VARIABLES AND METHODS Rules for Using static Variables There are two simple rules to remember when using static variables: Class methods can reference only the static variables, and never the instance variables. Instance methods can reference static and instance variables.

9.1 CLASS (STATIC) VARIABLES AND METHODS Class Constants By using the modifier final in conjunction with static, we create a class constant. The value of a class constant is assigned when the variable is declared, and it cannot be changed later. To illustrate the use of class constants, we modify the Student class again by adding two class constants: MIN_SCORE and MAX_SCORE.

9.1 CLASS (STATIC) VARIABLES AND METHODS // static variables and methods static final public int MIN_SCORE = 0; static final public int MAX_SCORE = 100; }

public class Student { private String name;... rest of the instance variables go here no changes in the methods up to this point... public void setScore (int i, int score){ // Limit the score to the interval [MIN_SCORE, MAX_SCORE] score = Math.max (MIN_SCORE, score); score = Math.min (MAX_SCORE, score); if (i == 1) test1 = score; else if (i == 2) test2 = score; else test3 = score; }... no changes in the methods here... The method max in class Math returns the maximum of its two parameters and min returns their minimum. We declare the two class constants as public because clients might like to access them.

9.1 CLASS (STATIC) VARIABLES AND METHODS Here is a segment of code that illustrates the Student class's new features: s = new Student(); s.setScore(1, -20); // Too small, will be set to MIN_SCORE s.setScore(2, 150); // Too large, will be set to MAX_SCORE s.setScore(3, 55); // Value is acceptable System.out.println (s);// Displays scores of 0, 100,and 55 System.out.println (Student.MIN_SCORE);// Displays 0 System.out.println (Student.MAX_SCORE);// Displays 100

Your assignment is to add the statements discussed previously to the Student class and test them in your TestStudent class. Student : 1)Add the statements to create, increment, and access the static variable studentCount (studentCount, setStudentCount, and getStudentCount) 2)Add the statements to create the class constants MIN_SCORE and MAX_SCORE TestStudent: Add the following code to the end of your current code in TestStudent System.out.println(Student.getStudentCount( ) + “ students where created”); System.out.println(“The minimum score is “+ Student. MIN_SCORE); System.out.println((“The maximum score is “+ Student.MAX_SCORE); TestStudent should now print: 3 students were created The minimum score is 0 The maximum score is 100 at the end.