/
Java Object Model   jGuru Java Object Model   jGuru

Java Object Model jGuru - PDF document

tatyana-admore
tatyana-admore . @tatyana-admore
Follow
482 views
Uploaded On 2014-11-16

Java Object Model jGuru - PPT Presentation

com All Rights Reserved Java Object Model 1 The Java Object Model Topics in this section include Java classes including the use of access rights inheritance method definitions constructors and instance versus class members Java pac ID: 12742

com All Rights Reserved Java

Share:

Link:

Embed:

Download Presentation from below link

Download Pdf The PPT/PDF document "Java Object Model jGuru" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

return type, even a constructor with arguments it is a good idea to specify a default(sometimes called copy) constructor--a constructor with no arguments (one iscreated for you automatically if you override methods defined in the superclass by simply redefiningthem. For example, to redefine the getName method in Manager, you could do thefollowing:class Manager { ... String getName() { return "manager " + name; }}The behavior of a Manager would now be different than a generic Employee withrespect to getName.Object Type EquivalenceRecall that class inheritance is an identity relationship in that a subclass is aspecialization of the superclass. You not true, however. on the string manager to whateverthe superclass defined getName to return.What if you define a variable member with the same name as a variable inheritedfrom the superclass? How can you access both of them? Use this.variable toaccess the variable in the current class and super.variable to access the variableinherited from the superclass.A more common variation of this problem arises when you define the argument ofa method to have the same name as a member variable. Again, the this keywordprovides a scope override to allow unambiguous references:class Employee { String name = null; void setName(String name) { // member var = argument this.name = name; }}Constructors and InheritanceConstructors are not automatically inherited by subclasses. does not specify statement may be variety of constructorswith different signatures that indirectly establish default values for state variables.Order of Initialization and Constructor The member variables of Employee are initialized.7. that code invoke,for example, method Marketeer.goOnVacation when the compiler was notaware of class Marketeer? The answer of course is that method calls are notbound to exact implementations by the compiler--method calls are bound toimplementations at run-time.Method invocation (or message send) expressions such as o.m(args) areexecuted as follows:1. Ask o to return its type (class name); call it T.2. Load T.class if have type Employee as far as the compiler is concerned--you could be referring to any subclass of Employee. It's not Employee cantake on (its subclasses) and late binding refers to the mechanism for achieving theflexibility.Access Rights to modified with public implies that any method in etc...Any nonmodified member of a class is visible to any method of any class withinthe same package, but no methods in classes outside the package may refer tothose members. For example, this makes sense that a classes within a package ! A protected member is visible to any class within the package and to anysubclass even if the subclass is defined in another package as long as thesuperclass is public. The Java language has explicit abstract classes, for example,// Java codeabstract class Shape { float angle = 0.0; public abstract void draw(); public void rotate(float degrees) { angle = degrees; draw(); }}All subclasses of Shape inherit the abstract method draw as well as rotate, whichdepends on draw:// Java codeclass Box extends Shape { public void draw() { // code to draw box at angle }}Abstract classes cannot be instantiated; describes T's intrinsic identity thereby implyingthat all subclasses of S are related. For example, if class Employee is ageneralization of both Manager and President, then Manager and Presidentmust be related. Any common behavior such as paySalary is generalized and viewed as a particular "perspective" of the data stored within. To walk anyenumeration, the following generic method can be used:void walk(Enumeration e) { while ( e.hasMoreElements() ) { System.out.println(e.nextElement()); }}The java.util.Vector class has a method called elements that returns an object ! Class List is visible to classes outside package LinkedList. For example, class Ais able to make List system normally insists that the directory structure of yourclass library match that of the language package hierarchy. For example, classLinkedList.Listmust exist in fileLinkedList/List.classstarting from one of the directories listed in your CLASSPATH environment variable.Your CLASSPATH should include '.', the current directory. operations on a the data structure explicitly(isolating implementation from interface), we have made the data static, thusmaking it invisible outside of the file.Naturally, the problem with this scheme is that only one list manager can becreated. To solve this, you would put the data within a struct and then list meansthat it is now really List.add()! Function names are more general in Java.! list_init() list_add(mylist, "element 1");versusmylist.add("element 1");In C you write function-centric code. In Java, you write data-centric code. That is,"execute this code using this data" versus "here's some data...operate on it".Object-oriented programmers typically go further than this to say "ask the list toadd "element 1" to itself." Another common terminology (from Smalltalk) woulddescribe this operation as "send the