Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Advertisements

Chapter 12 OOP: Creating Object- Oriented Programs Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Fields, Constructors, Methods
12-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Road Map Introduction to object oriented programming. Classes
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Object-oriented Programming Concepts
Terms and Rules Professor Evan Korth New York University (All rights reserved)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
ASP.NET Programming with C# and SQL Server First Edition
Chapter 13: Object-Oriented Programming
Chapter 10 Classes Continued
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
An Object-Oriented Approach to Programming Logic and Design
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
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™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 5 Introduction to Defining Classes
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
OOP Basics Classes & Methods (c) IDMS/SQL News
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
Object-Oriented Programming: Classes and Objects.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Introduction to Object-oriented Programming
Inheritance and Polymorphism
Chapter 3: Using Methods, Classes, and Objects
Can perform actions and provide communication
Chapter 3 Introduction to Classes, Objects Methods and Strings
Can perform actions and provide communication
Interfaces.
Advanced Java Programming
Dr. Bhargavi Dept of CS CHRIST
Can perform actions and provide communication
Object-Oriented Programming: Classes and Objects
Object-Oriented Programming: Inheritance and Polymorphism
Object Oriented Programming in java
Introduction to Classes and Objects
Presentation transcript:

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 2 McGraw-Hill/Irwin Objectives Understand more thoroughly the characteristics of object- oriented programming. Know when to appropriately use public and private variables and methods. Create your own classes. Instantiate objects of your class. Write methods that return values and accept input values.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 3 McGraw-Hill/Irwin Creating Classes Applets created in a single class become very large and difficult to manage. The best way is to break up the tasks into smaller objects with each responsible for a specific task. Learning to separate the processing from the user interface.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 4 McGraw-Hill/Irwin Encapsulation Encapsulation refers to combination of properties and methods in a single unit. Encapsulation allows an object to maintain the integrity of the data by “hiding” it from the other objects. Think of an object as a “black box” that carries out a task. The only way to gain access to any of the properties and methods of an object is to use those properties and methods specifically declared public. Each object keeps control of its own components, variables, and methods.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 5 McGraw-Hill/Irwin Inheritance You can add enhancements to an existing class without modifying the original. The existing class is known as base class or superclass. Applet class is the base class and the new class is the subclass. The Applet class already has the init method; but the init method you write executes not the one in the Applet class.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 6 McGraw-Hill/Irwin Inheritance Continued This is a way to modify the existing method in the base class with yours in the subclass by giving the same name to the method. This is called overriding. You can also achieve the affect of deleting a method in the base class by creating a method in your subclass with same name and having no statements inside (empty method).

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 7 McGraw-Hill/Irwin Interfaces and Inner Classes In Java, you can inherit only one superclass unlike C++. An interface is similar to a class but it exposes a set of methods. Therefore, you must override all the methods you use and don’t use. When you implement an interface, you are guaranteeing that your class will contain all the methods, which are in the interface.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 8 McGraw-Hill/Irwin Although you can have only one superclass, you can implement as many interfaces you want. This gives you access to a lot of methods, in addition to the superclass. You can create an interface the same way you create a class by replacing the keyword word class with interface. An interface can contain methods and constants but no variables. An inner class is a class defined within another class. Interfaces and Inner Classes Continued

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 9 McGraw-Hill/Irwin Abstract Classes These classes are solely written for the purpose of inheritance. It can be a superclass to more than one subclass. An abstract class contains abstract methods that do not contain any statements, called abstract methods. You cannot instantiate an object from the abstract class. You can only inherit the abstract class.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 10 McGraw-Hill/Irwin Polymorphism Polymorphism refers to more than one method having identical names. An example of polymorphism is the methods of the Component class being the superclass are being inherited for both the Label class and TextField class. For example, the setText and getText methods for work appropriately for both the Label and TextField classes.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 11 McGraw-Hill/Irwin Objects The main idea behind OOP is to create reusable code. An object is a “thing” that characteristics and behaviors. When you instantiate the object from a class the constructor method in the class executes. You can also declare a button with no label. This illustrates that there is more than one constructor. The constructor with the label executes one constructor method and the one.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 12 McGraw-Hill/Irwin Your Own Objects You can create your own classes and then instantiate multiple objects in another class.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 13 McGraw-Hill/Irwin Creating a New Class Your project can have multiple classes and then instantiate these classes in another class. Each class can be saved as a separate file with a.java extension and then store all the files in the same folder.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 14 McGraw-Hill/Irwin Public vs. Private As you work with multiple classes the concept of public or private access becomes more important. Until now you had only one class and everything was public and it was not critical. The concept of Encapsulation – well designed class keeps its details hidden form other classes.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 15 McGraw-Hill/Irwin You are using the “get” and “set” methods to gain access. Inside a class, you can declare variables and methods to be public or private. Public variables and methods can be referenced by an object instantiated from the class. Only statements within the class itself can reference variables and methods declared private. Good programming practice is to make all your class variables private, which can be accessed by only public methods. Public vs. Private Continued

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 16 McGraw-Hill/Irwin Returning Values form a Method So far, we have been coding methods that do not return a value. In this case, void means we are not returning a value.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 17 McGraw-Hill/Irwin The Method Header – General Format public|private ReturnType MethodName(Arguments)

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 18 McGraw-Hill/Irwin The Method Header – Examples public float getRate() public String getDescription() public float calculateInterest(float fltPrincipal, float fltRate; float fltTime private void doSomething()

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 19 McGraw-Hill/Irwin A return Statement A method that returns a value must contain a return statement.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 20 McGraw-Hill/Irwin The return Statement – General Format return Value;

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 21 McGraw-Hill/Irwin The return Statement - Examples return fltRate; return “Really big deal”; return intCount; return 15; return fltHours * fltRate

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 22 McGraw-Hill/Irwin Passing Arguments to a Method You can declare a method header that is expecting arguments and pass the arguments when the method is called. Parameters Let us look at an example: public float calculateExtended(int intQuantity, float fltPrice) { return intQuantity * fltPrice; } Arguments fltExtendedPrice = calculateExtended(intNumber, fltUnitPrice); The arguments and the parameters don’t have the same name and that is okay.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 23 McGraw-Hill/Irwin Dividing an Applet Class We will create two classes: The Applet class contains the items and functions that relate to the user interface, which includes the buttons, labels, and textfields. The other class will have the variables and the methods to do the calculation.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 24 McGraw-Hill/Irwin Constructors A constructor is a very important method of a class. A constructor is method that is automatically called when a new object is created from the class. Declaring a class only creates a blueprint for a type of an object; it does not create a memory location for an actual object. You allocate memory when you use the new keyword to create an instance of the class.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 25 McGraw-Hill/Irwin Adding a Constructor Create the constructor with the same name as the class without using the keywords public or private. The constructor must always be public and that is the default. It is not a good idea to have the getText method directly for the inputs because if there is an empty string, it will throw an exception. You cannot have the instantiated class display an error message to the user as all the interaction is done in the applet class.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 26 McGraw-Hill/Irwin Obtaining Values from a Private Class Variables It is always important to even keep the method that calculates private. You can create a public method containing the prefix get to retrieve information.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 27 McGraw-Hill/Irwin Using a Class Variable for a Total A class variable is a variable that only one copy of that variable exists, no matter how many times you instantiate the object. To declare a class variable, you must use the keyword static. If you wish to keep a running total, the class variable will not be reinitialized to zero each time you instantiate the object.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 28 McGraw-Hill/Irwin Creating a Class For Formatting You will use formatting in probably all your classes, how about creating class that will take care of all the formatting needs. Formatting a float and double for a different number of decimal positions the user requires. Formatting for a currency for float and double data types. The class will be called LocalFormat and will be used throughout the book.