Java Summary

Reviews
Shared by: Anna Uls
Categories
Stats
views:
96
rating:
not rated
reviews:
0
posted:
6/7/2009
language:
English
pages:
0
Java Summary • • • • • • • • • OOP classes, data and methods definition and use public and private inheritance - extends keyword method overloading method overriding threads and dynamic memory libraries Objects • OOP - object oriented programming • code built from objects • Java these are called classes • Each class definition is coded in a separate .java file • Name of the object must match the class/object name Classes eg. Fruit.java contains the lines: class Fruit{ int grams; int cals_per_gram; } Methods ... Class Fruit{ int grams; int cals_per_gram; int total_calories(){ return(grams*cals_per_gram); } } • brackets () signify a method rather than data Using objects • Here, code in one class creates an instance of another class and does something with it … Fruit plum=new Fruit(); int cals; cals = plum.total_calories(); • Dot operator allows you to access (public) data/methods inside Fruit class Public/private • Methods/data may be declared public or private meaning they may or may not be accessed by code in other classes … • Good practice: – keep data private – keep most methods private • well-defined interface between classes - helps to eliminate errors Creating objects • Following code creates an instance of the Fruit class Fruit plum; • defines the plum object plum = new Fruit(); • creates it in memory • the content of the Fruit class must be defined in another file Fruit.java Constructors • The line plum = new Fruit(); • invokes a constructor method with which you can set the initial data of an object • You may choose several different type of constructor with different argument lists eg Fruit(), Fruit(a) ... Overloading • Can have several versions of a method in class with different types/numbers of arguments • Fruit(){grams=50;} Fruit(a,b){ grams=a;cals_per_gram=b; } • By looking at arguments Java decides which version to use Inheritance ... • Important feature of OOP - new classes can be based on existing classes eg. Could define a `specialized’ type of Fruit class called Citrus … • Has all methods of Fruit plus possibly some new ones eg class Citrus extends Fruit{ void squeeze(){….} } Inheritance II • How to use … eg. Citrus lemon = new Citrus(); lemon.squeeze(); lemon.total_calories(); • old methods exist alongside new methods … Overriding • Even more powerful concept of OOP – can override the functionality of one method in a descendant class • eg. Add method peel() to Fruit class. Since Citrus extends Fruit this method will also be available to an instance of Citrus • But can redefine content of peel() inside of Citrus - the new definition hides the earlier ... Threads ... • Java allows code in some objects to be run like a separate program - it is given its own thread of execution • eg. Runner = new Thread(sim) • sim is name of class containing a run() method which is responsible for eg a simulation • class sim implements Runnable{ ..run(){…}} Libraries • Java comes with libraries for creating GUIs and network applications and for embedding in Web pagesjava.applet.Applet • eg import java.awt.*; • compile to byte code - can be run by any system with a Java interpreter - portable! • Relatively robust and secure

Related docs
Java
Views: 79  |  Downloads: 13
webDynpro for java
Views: 798  |  Downloads: 43
Java Basics
Views: 475  |  Downloads: 63
Java
Views: 25  |  Downloads: 3
Introduction to Java
Views: 64  |  Downloads: 17
Collections in Java
Views: 162  |  Downloads: 18
Introduction to Java
Views: 160  |  Downloads: 56
Java_Applets.pdf
Views: 138  |  Downloads: 30
JAVA
Views: 154  |  Downloads: 0
Basic Java Summary Sheet
Views: 13  |  Downloads: 2
design patterns
Views: 840  |  Downloads: 64
JAVA
Views: 16  |  Downloads: 6
brief history of java
Views: 579  |  Downloads: 53
Java and the JVM
Views: 42  |  Downloads: 3
premium docs
Other docs by Anna Uls
eToys Inc Ammendments and Bylaws
Views: 182  |  Downloads: 0
Employee Compensation Record
Views: 236  |  Downloads: 6
Sexual harrassment aids
Views: 374  |  Downloads: 14
pos020
Views: 191  |  Downloads: 0
CERTIFICATE OF INSTALLATION
Views: 212  |  Downloads: 1
Sample Agreement to Form Business Entity
Views: 471  |  Downloads: 6