Course List: http://www.c-jump.com/bcc/
The inheritance hierarchy for Swing forms and controls
Every Java object inherits several methods of the Object class
(or original methods can be overridden in a subclass of the Object class.)
Two of the most common methods are
toString( )
equals( )
The toString method returns the class name of the object followed by its memory location.
It has been overridden in the String class.
The default equals( ) method returns true if
the object for which the method is invoked, and
the object passed as equals( ) parameter
share the same memory location.
The equals( ) method is overridden in the String class, and in the ArrayList class.
The finalaize( ) method provides "last minute" chance for processing an object which is about to be destroyed by the java garbage collector.
java.lang.Object toString() equals( Object ) getClass() clone() hashCode() finalize()
private public protected //default
Java annotations provide metadata about a particular construct in the program.
@Override // method declaration goes here
Annotation's role is similar to program comments. Annotations can be useful to the compiler, various software tools, and program documentation.
Possible areas of use:
DVD burning/production
Bookstore
Game shop
Shipping warehouse
_______________________________________________________ | |_____________Useful classes______________| | Data | | | | Software | | | Attribute | Product | Book | DVD | Download | Game | |-------------+---------+------+-----+----------+-------| | code | x | description | x | price | x | weight | x | author | x | publisher | x | version | x | releaseDate | x `-------------+------------------------------------------
To declare a subclass
public class SubclassName extends SuperClassName{}
To call a superclass constructor
super( argumentList )
To call a superclass method
super.methodName( argumentList )
java.lang.Class
Common method
getName()
|
|
How the equals method of the Object class works:
(a) Both variables refer to the same object:
Product product1 = new Product(); Product product2 = product1; if ( product1.equals( product2 ) ) // true
(b) Both variables refer to different objects that store the same data:
Product product1 = new Product(); Product product2 = new Product(); if ( product1.equals( product2 ) ) //false
To override the equals() method of the Object class, add your own equals() method to the class.
For example, the Product class could implement the method as follows:
@Override public boolean equals( Object object ) { if ( object instanceof Product ) { Product product2 = (Product) object; if ( code.equals( product2.getCode() ) && description.equals( product2.getDescription() ) && price == product2.getPrice() ) { return true; } } return false; }//equals
The abstract keyword
restricts class of objects by preventing their instantiation
forces abstract methods to be implemented in the subclasses
If a class contains an abstract method (with no body) then the class must be declared as an abstract class.
To allow object instantiation, all abstract methods must be implemented in the subclass which inherits from the abstract superclass.
public abstract class Product
{
private String code;
private String description;
private double price;
// regular constructors and methods for instance
// variables
@Override
public String toString()
{
return "Code: " + code + "\n" +
"Description: " + description + "\n" +
"Price: " + this.getFormattedPrice()
+ "\n";
}
// an abstract method
abstract String getDisplayText();
}//class Product
A class that inherits the abstract Product class
public class Book extends Product
{
private String author;
// regular constructor and methods for the Book class
// implement the abstract method
@Override
public String getDisplayText()
{
return super.toString() +
"Author: " + author + "\n";
}
}//class Book
The final modifier can
prevent a class from being sub classed (i.e. inherited from)
prevent a method from being overridden in a subclass
prevent a parameter passed into a method from being modified
public final class Book extends Product { // all methods in the class are automatically final }//class Book
public final String getVersion()
{
return version;
}
public void setVersion( final String version )
{
// version = "new value"; // not allowed
this.version = version;
}