1 / 5

Class Object and class Class

Class Object and class Class. Two fundamental classes of the Java API. Class Object. Class Object is the root of the object hierarchy Behavior defined in Object is inherited by all classes Subclasses are supposed to override most methods (or live with the default implementations)

mick
Télécharger la présentation

Class Object and class 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. Class Object and class Class Two fundamental classes of the Java API

  2. Class Object • Class Object is the root of the object hierarchy • Behavior defined in Object is inherited by all classes • Subclasses are supposed to override most methods (or live with the default implementations) • Some methods of class Object • String toString() • boolean equals(Object obj) • int hashCode() • Class getClass() • Returns an object describing the class of the present object.

  3. class Class • “Instances of the class Class represent classes and interfaces in a running Java application” [Java 2 Platform Standard Edition 5.0 API Specification]. • Instances of class Object are objects. • Instances of class Class are objects describing classes. • Some methods of class Class • static Class<?> forName(String className) • Returns a Class object for the class with the className. • Used in JDBC to load a driver. • T newInstance() • Constructs an object of this class.

  4. Reflection • With the reflection API you can create and manipulate objects of classes where the only thing you have is a String with the name of the class. • Constructing an object from class’ name Class class1 = Class.forName("javax.swing.JButton"); Object obj = class1.newInstance(); • The class must have a no-arg constructor • Used by tools (including NetBeans), servers, etc. to load classes and construct objects from the classes. • Using the reflection API (class Class and other classes) • Constructor[] getConstructors() • Method[] getMethods() • FieldgetField(String name) • Package java.lang.reflect

  5. References • Niemeyer & KnudsenLearning Java, 3rd edition, O’Reilly 2005 • 7. Working with Objects and Classes, page 193-213

More Related