html5-img
1 / 84

How to work with the Object class

How to work with the Object class. The methods of the Object class Description The object class is the superclass for all classes. As a result, you can call its methods from any object of any class.

anoush
Download Presentation

How to work with the Object class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. How to work with the Object class The methods of the Object class Description • The object class is the superclass for all classes. As a result, you can call its methods from any object of any class. • When coding classes, it’s a common practice to override the toString and equals methods so they work appropriately for each class

  2. How to work with the Object class The methods of the Object class Description(continue) • The hash code for an object is a hexadecimal number that identifies the object’s location in memory • In general, you don’t need to code a finalize method for an object. That’s because Java’s garbage collector automatically reclaims the memory of an object when it needs to. Before it does, it calls the finalize method of the object

  3. How to work with the Object class How to cast objects Description • To use some of the methods of the Object class, you need to be able to cast any type of object to an Object object, and you need to be able to cast an Object object to any other of object. • To cast an object up the inheritance chain(from subclass to super class), you code a simple assignment statement

  4. How to work with the Object class How to cast objects Description (continue) • To cast an object down the inheritance chain(from superclass to subclass), you need to code the class name within parentheses to confirm the assignment statement. This type of cast will only work when the object is an instance of the intended class For some methods, you need to code a parameter that accepts an Object object. Then, you can pass any type of object to that method, and the method can use the instanceof operator to determine what type of object has been passed to the method

  5. How to work with the Object class How to override the toString method Description • The toString method of the Object class returns a String object that contains the class name, followed by the @ symbol, followed by the hash code for this object. • To override the toString method of the Object class, code a toString method in the class that you’re coding as shown above.

  6. How to work with the Object class How to override the toString method Description (continue) • At runtime, Java automatically calls the toString method of an object when you pass any object to the println method of the System.out object or when you use the plus operator (+) to concatenate an object with a string

  7. How to work with the Object class How to override the equals method Description • To test if two objects point to the same space in memory, you can use the equals method of the Object class • To test if two objects store the same data, you can override the equals method in the subclass so it tests whether all instance variables in the two objects are equal.

  8. How to work with the Object class How to use the Class to get information about an Object object Description • While a program is running, Java uses run-time identification(RTTI) to keep track of the classes that each object belongs to and to store detailed information about all loaded classes, arrays, and primitive types. You can use the methods of the Class class to access this information. • The two methods(see next slide) are only two of the more than 30 methods of the Class class

  9. How to work with the Object class How to use the Class to get information about an Object object Description(continue) • The methods of the Class class can be used with the classes of the java.lang reflect package to get detailed information about the fields, constructors, and methods of an object. This is the basis for working with JavaBeans, which your applications to dynamically interact with other classes at run-time

  10. More skills for coding classes and methods How to code a method that throws an exception Description • When a method includes code that may throw an exception, the method can catch the exception or throw it to the class that uses the method. However, not all exceptions need to be caught or thrown • A checked exception is a type of exception that’s checked by the compiler. When you use a method that throws a checked exception, you must supply code that throws or catches that exception or you won’t be able to compile your program

  11. More skills for coding classes and methods How to code a method that throws an exception Description • To throw an exception, you use the throws keyword to code a throws clause in the method declaration • The IOException is a type of checked exception that’s thrown by classes that work with file input and output • For more information about catching and throwing exceptions, see ch10

  12. More skills for coding classes and methods How to work with abstract classes and methods Description • An abstract class serves as a template that can be inherited by the subclasses. However, you can’t create an object directly from an abstract class. To declare an abstract class, use the abstract keyword in the class declaration. • When a non-abstract subclass inherits an abstract class, all abstract methods in the abstract class must be overridden in the subclass.

  13. More skills for coding classes and methods How to work with abstract classes and methods Description • To declare an abstract method, use the abstract keyword in the method declaration, don’t include braces, and end the statement with a semicolon • An abstract class may or may not contain abstract methods. However, any class that contains an abstract method must be declared as abstract • All abstract classes must be declared as public while abstract methods can be declared as public or protected

  14. More skills for coding classes and methods How to work with final classes and methods Description • To prevent a class from being inherited, you can create a final class by using the final keyword in the declaration of the class • To prevent subclasses from overriding a method of a superclass, you can create a final method by using the final keyword in the declaration of the method. In addition, all methods in a final class are automatically final methods

  15. More skills for coding classes and methods How to work with final classes and methods Description • To prevent a method from assigning a new value to a parameter, you can use the final keyword in the method declaration to declare a final parameter. The, if a statement in the method tries to assign a new value to the final parameter, the compiler will report an error.

  16. More skills for coding classes and methods How to work with access modifiers Description • To encapsulate the data in your classes, you code private instance variables. Then, you code public set and get methods that set and return the values of the private instance variables • You can use the public keyword to code static constants. That way, other classes can call those constants from the class • You can use the private keyword to code methods that are used only within the current class

  17. More skills for coding classes and methods How to work with access modifiers Description • You can use the protected keyword to code fields and methods that can be accessed only by other classes in the same package and by any subclasses. • If you don’t code an access modifier, your fields and methods will be available only to other classes in the same package

  18. More skills for coding classes and methods How primitive types and objects are passed to a method Description • When a variable with a primitive type is passed to a method, it is passed by value. That means the method can’t change the value of the variable itself. Instead, the method must return a new value that gets stored in the variable • When an object is passed to a method, it is passed by reference. That means that the method can change the data in the object itself so a new value doesn’t need to be returned by the method

More Related