Java Classes
Download
Report
Transcript Java Classes
PART 1:
OBJECT RECAP
February 26, 2013
Methods and Memory
• Pass by value
• Pass by reference
• Demos
PART 2:
JAVA INHERITANCE
February 26, 2013
Javax.swing.JFrame
Superclass
Public fields
and methods
Subclass
Code that uses
inherited fields
and methods
New methods
HIDE_ON_CLOSE
EXIT_ON_CLOSE
void
void
void
void
void
setTitle(String title)
setLocation(int x, int y)
setSize(int h, int w)
setResizable(boolean b)
setDefaultCloseOperation(int i)
murach.presentation.ProductFrame
setTitle("Product");
setLocation(10, 10)
setSize(200, 200);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE)
void actionPerformed(ActionEvent e)
void keyPressed(KeyEvent e)
Inheritance hierarchy for Swing
java.lang
java.awt
javax.swing
Object
Component
Container
JComponent
Window
Frame
Swing classes for
buttons, labels, text
boxes, etc.
JFrame
Object Class
• java.lang.Object
• All java objects, even the ones you make,
automatically inherit from the Object class
• Methods
• toString()
• equals(Object)
• getClass()
• clone()
• hashCode()
• finalize()
Product
void setCode(String c)
String getCode()
void setDescription(String d)
String getDescription()
void setPrice(double p)
double getPrice()
String getFormattedPrice()
String toString()
Book
Software
void setCode(String c)
String getCode()
void setDescription(String d)
String getDescription()
void setPrice(double p)
double getPrice()
String getFormattedPrice()
String toString()
void setCode(String c)
String getCode()
void setDescription(String d)
String getDescription()
void setPrice(double p)
double getPrice()
String getFormattedPrice()
String toString()
void setAuthor(String a)
String getAuthor()
void setVersion(String v)
String getVersion()
Access Modifiers
Modifier
public
protected
no keyword
coded
private
Class Package Subclass World
Override annotation
• @Override
• // method declaration goes here
Syntax – for subclasses
• To declare a subclass
• public class SubclassName extends SuperClassName{}
• To call a superclass constructor
• super(argumentList)
• To call a superclass method
• super.methodName(argumentList)
Polymorphism
Book b = new Book();
b.setCode("java");
b.setDescription("Murach's Beginning Java");
b.setPrice(49.50);
b.setAuthor("Steelman");
Software s = new Software();
s.setCode("txtp");
s.setDescription("TextPad");
s.setPrice(27.00);
s.setVersion("4.7.3");
Product p;
p = b;
System.out.println(p.toString());
p = s;
System.out.println(p.toString());
// calls toString from
// the Book class
// calls toString from
// the Software class
Getting Object information
• The Class class
• java.lang.Class
Product p = new Book();
• getName()
// create a Book object and
// assign it to a Product
// variable
Class c = p.getClass(); // get the Class object for the
// product
System.out.println("Class name: " + c.getName());
// print the object type
Testing an object’s type
Product p = new Book(); // create a Book object
if (p.getClass().getName().equals("Book"))
System.out.println("This is a Book object");
• Another (easier) way
Product p = new Book(); // create a Book object
if (p instanceof Book)
System.out.println("This is a Book object");
Casting Objects
• Java can implicitly cast subclass to superclass.
• You must explicitly cast superclass to subclass.
Book b = new Book();
b.setCode("java");
b.setDescription("Murach's Beginning Java");
b.setAuthor("Andrea Steelman");
b.setPrice(49.50);
Product p = b;
//
//
p.setDescription("Test"); //
//p.setAuthor("Test");
//
//
b = (Book) p;
b.setAuthor("Test");
cast Book object to a
Product object
OK - method in Product class
not OK - method not in
Product class
// cast the Product object back
// to a Book object
// OK - method in Book class
Product p2 = new Product();
Book b2 = (Book) p2;
// will throw ClassCastException
// because p2 is a Product object
// not a Book object
equals method of Object class
• Both variables refer to the same object
Product product1 = new Product();
Product product2 = product1;
if (product1.equals(product2))
// expression returns
// true
• Both variables refer to different objects that store the same data
Product product1 = new Product();
Product product2 = new Product();
if (product1.equals(product2))
// expression returns
// false
abstract keyword
• An abstract class can’t be instantiated.
• Does not require all methods to be abstract.
• Abstract methods must be public.
• Abstract class doesn’t require abstract methods, but if
there is an abstract method then the class must be as
well.
Abstract class
public abstract class Product {
private String code;
private String description;
private double price;
@Override
public String toString()
return "Code:
"Description:
"Price:
// regular constructors
// and methods for
// instance variables
{
" + code + "\n" +
" + description + "\n" +
" + this.getFormattedPrice()
+ "\n";
}
// an abstract method
abstract String getDisplayText();
}
Inheriting abstract 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";
}
}
final keyword
• Prevent others from inheriting, overriding or changing
classes, methods or parameters.
• Slight performance benefits.
Using final
public final class Book extends Product
{
// all methods in the class are automatically final
}
public final String getVersion()
{
return version;
}
public void setVersion(final String version)
{
// version = "new value"; // not allowed
this.version = version;
}
Product App (Things to look for)
• ch08_Product
• protected static variable for Product superclass
• @override in toString() for Product superclass
• Extends in Book subclass
• Call superclass constructor in Book constructor
• Access static counter variable in Product superclass from
Book subclass constructor
• Call superclass method in Book toString() override
• toString() versions
• Product superclass
• Book subclass
• Software subclass