Chapter 8 Classes and Objects

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Inner Classes. Lecture Objectives Learn about inner classes. Know the differences between static and non- static inner classes. Designing and using inner.
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
©2004 Brooks/Cole Chapter 10 More on Classes. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Object assignment With primitive types, setting.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Syllabus (1) WeekChapters 1Introduction to the course, basic java language programming concepts: Primitive Data Types and Operations 1, 2 2Methods, Control.
Class and Object. Class vs Object Let us consider the following scenario… Class defines a set of attributes/fields and a set of methods/services. Object.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
Java Interfaces. Interfaces An interface is something like an extreme case of an abstract class – However, an interface is not a class – It is a type.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
C11, Implications of Inheritance
Topic: Classes and Objects
Chapter 5: Enhancing Classes
NESTED CLASSES REFLECTION PROXIES.
Java Programming: Guided Learning with Early Objects
Interface.
03/10/14 Inheritance-2.
CIS3023: Programming Fundamentals for CIS Majors II
Continuing Chapter 11 Inheritance and Polymorphism
Advanced Programming in Java
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Interfaces and Inner Classes
CS 302 Week 11 Jim Williams, PhD.
Advanced Programming in Java
More Object Oriented Programming
User-Defined Classes and ADTs
Computer Science II Exam 1 Review.
Chapter 9 Inheritance and Polymorphism
Corresponds with Chapter 7
Classes & Objects: Examples
Sampath Kumar S Assistant Professor, SECE
Objects and Classes Creating Objects and Object Reference Variables
Method of Classes Chapter 7, page 155 Lecture /4/6.
Chapter 11 Inheritance and Polymorphism
Chapter 9 Objects and Classes
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
CS 200 More Classes Jim Williams, PhD.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Review for Midterm 3.
Presentation transcript:

Chapter 8 Classes and Objects Passing parameters to methods The Object Class Inner Classes

Passing parameters to method Java uses exactly one mode of passing parameters: pass by value. When passing by value of a primitive data type, the value of the actual parameter is passed. When passing a parameter of a reference type, the reference of the object is passed.

Example: Test passing object public class TestPassingObject() { public static void main(String[] args) { Circle new myC = Circle(); // default int n = 3; printRadii(myC, n); System.out.println(“main(): radius is ” + myC.getRadius()); System.out.println(“main(): n is ” + n); } public static void printRadii(Circle c, int times) { while(times >= 1) { System.out.println(“radius is ”+c.getRadius()); c.setRadius(c.getRadius()+1); times--;

Example: Output Output: ... printRadii(myC, n); System.out.println(“main(): radius is ”+myC.getRadius()); System.out.println(“main(): n is ” + n); Output: radius is 1.0 radius is 2.0 radius is 3.0 main(): radius is 4.0 main(): n is 3

Example Review main() printRadii() times n 3 3 c myC Pass by value value is 3 c myC value is reference for the object myC : Circle radius = 1

Passing parameters to method Parameter value of a primitive data type. The value of n (3) is passed to times. Inside the method, the content of times is changed; it does not affect the content of n. Parameter value of a reference type (object or array): Parameter c contains a reference for the object that is also referenced via myC. Therefore, changing properties of the object through c inside printRadii() method has the same effiect as doing so outside the method through variable myC.

The Object Class The Object class is the root of all Java classes. The boolean equals() method compares the contents of two objects. The String toString() method returns a string representation of the object. The clone() method copy objects.

The equals() method The equals() method compares the contents of two objects. The default implementation of the equals method in the Object class is as follows: public boolean equals(Object obj) { return (this == obj); }

The equals() method, cont. Example 1: public class Circle { public boolean equals(Circle c) { return (this.radius == c.getRadius()); } Example 2: public class Rectangle { public boolean equals(Rectangle r) { return (this.width == r.getWidth() && this.length == r.getLength());

The toString() method The String toString() method returns a string representation of the object. The default implementation returns a string consisting of a class name of which the object is an instance, the at sign (@), and a number representing this object. Example Circle c = new Circle(); System.output.println(c.toString()); // Circle@1de167

The toString() method, cont. public class Circle extends Shape { ... public String toString() { return "[Circle] radius = " + radius; } Example Circle c = new Circle(); System.out.println(c.toString()); // [Circle] radius = 1.0

The clone() method newObject = someObject.clone(); To create a new object with separate memory space, you need to use the clone() method, as follows: newObject = someObject.clone(); NOTE: Not all objects can be cloned. For an object to be cloneable, its class must implement the java.lang.Cloneable interface (more later).

Shallow copy The clone() method in the Object class copies each field from the original object to the target object. If the filed is of primitive type, its value is copied . If the field is of an object, the reference of the field is copied. This is referred as shallow copy.

Deep copy Shallow copying is potentially dangerous action, it can cause side effects. If you want to perform a deep copy, you can override the clone() method with custom cloning operations instead of invoking super.clone();

Inner Classes Inner class: A class is a member of another class. Advantages: In some applications, you can use an inner class to make programs simple. An inner class can reference the data and methods defined in the outer class in which it nests, so you do not need to pass the reference of the outer class to the constructor of the inner class. But private members of inner class ae not visible for Inner class. Inner class cannot be public.

Inner Classes (cont.) An inner (or nested) class is only for supporting the work of its containing outer class, and it cannot be used by other classes. Inner classes can make programs simple and concise. Many Java development tools use inner classes to generate adapters for handling events.

Inner Class: Example public class A { private int data; public void method() { // Do something InnerClass a = new InnerClass(); a.innerMethod(); } // An inner class class InnerClass { public void innerMethod() { // Directly reference field b and method // in its outer class data++; method(); //