This chapter is concerning with following tips:
1. basic sense of inheritance of class
2. Methods (no-parameter and having parameters) inheritance
3. use of class Decimalformat
Referred to:
JDK 6 Documentation
Java How to Program by H. M. Deitel
1. Subclass inherits the attributes and behaviors of its superclass
Firstly
Subclass inherits the attributes and behaviors of its superclass, then we can add attributes and behaviors or override superclass behaviors to customize the class to meet our needs.
This means that the public interface of subclass (e.g., class Circle in following demo program) includes the superclass methods as well as the subclass methods itself (e.g., setRadius, get Radius, area, toString and constructors).
2. Methods (no-parameter and having-parameters) inheritance
Subclass should inherit all behaviors of its superclass as needed, particularly all constructors (without and having parameters) of its superclass.
3. DecimalFormat
public class DecimalFormat extends NumberFormat
DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, and Indic digits. It also supports different kinds of numbers, including integers (123), fixed-point numbers (123.4), scientific notation (1.23E4), percentages (12%), and currency amounts ($123).
-----------------------------------------------------------------------------------------
Related testing classes include:
- Point class extends from Object
- Circle class extends from Point class
- main class
- // Point.java
- package newpackage1;
- public class Point {
- protected int x, y; //coordinates of the Point
- // no-argument constructor
- public Point() { setPoint(0, 0); }
- public Point(int a, int b) { setPoint(a, b); }
- // constructor
- public void setPoint(int a, int b)
- {
- x = a;
- y = b;
- }
- // get x coordinate
- public int getX() { return x; }
- // get y coordinate
- public int getY() { return y; }
- // convert the point into a String representation
- @Override
- public String toString()
- { return "[" + x + ", " + y + "]"; }
- }
-
- // Circle.java
- package newpackage1;
- public class Circle extends Point { // inherits from Point
- protected double radius;
- // no-argument constructor
- public Circle()
- {
- // implicit call to superclass constructor
- setRadius(0);
- }
- // constructor
- public Circle(double r, int a, int b)
- {
- super(a, b); // call the superclass constructor
- setRadius(r);
- }
- // set radius of Circle
- public void setRadius(double r)
- { radius = (r >= 0.0 ? r : 0.0); }
- // get radius of Circle
- public double getRadius() { return radius; }
- // calculate area of Circle
- public double area()
- { return Math.PI * radius * radius; }
- // convert the Circle to a String
- public String toString()
- {
- return "Center = " + super.toString() +
- "; Radius = " + radius;
- }
- }
-
- // main.java
- import java.text.DecimalFormat;
- import javax.swing.JOptionPane;
- import newpackage1.Circle;
- public class Main {
- public static void main(String[] args) {
- Circle c = new Circle(2, 88, 110);
- DecimalFormat precision1 = new DecimalFormat("0.00");
- String output;
- output = "X coordinate is " + c.getX() +
- "/nY coordinate is " + c.getY() +
- "/nRadius is " + c.getRadius();
- c.setRadius(5.5);
- c.setPoint(2, 9);
- output = "/n/nThe new location and radius of c are/n" + c +
- "/nArea is " + precision1.format(c.area());
- JOptionPane.showMessageDialog(null, output,
- "Demonstrating Class Circle",
- JOptionPane.INFORMATION_MESSAGE);
- System.exit(0);
- }
- }
|