Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes."— Presentation transcript:

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

2 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.

3 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.

4 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.

5 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.

6 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).

7 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.

8 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

9 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.

10 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.

11 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.

12 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.

13 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.

14 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.

15 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

16 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.

17 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)

18 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()

19 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.

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

21 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

22 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.

23 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.

24 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.

25 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.

26 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.

27 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.

28 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.


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

Similar presentations


Ads by Google