Java - Class Inheritance 090906

 

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

 

  1. // Point.java
  2. package newpackage1;
  3. public class Point {
  4.     protected int x, y; //coordinates of the Point
  5.     // no-argument constructor
  6.     public Point() { setPoint(0, 0); }
  7.     public Point(int a, int b) { setPoint(a, b); }
  8.     // constructor
  9.     public void setPoint(int a, int b)
  10.     {
  11.         x = a;
  12.         y = b;
  13.     }
  14.     // get x coordinate
  15.     public int getX() { return x; }
  16.     // get y coordinate
  17.     public int getY() { return y; }
  18.     // convert the point into a String representation
  19.     @Override
  20.     public String toString()
  21.     { return "[" + x + ", " + y + "]"; }
  22. }
  23.  
  24. // Circle.java
  25. package newpackage1;
  26. public class Circle extends Point {  // inherits from Point
  27.     protected double radius;
  28.     // no-argument constructor
  29.     public Circle()
  30.     {
  31.         // implicit call to superclass constructor
  32.         setRadius(0);
  33.     }
  34.     // constructor
  35.     public Circle(double r, int a, int b)
  36.     {
  37.         super(a, b);  // call the superclass constructor
  38.         setRadius(r);
  39.     }
  40.     // set radius of Circle
  41.     public void setRadius(double r)
  42.     { radius = (r >= 0.0 ? r : 0.0); }
  43.     // get radius of Circle
  44.     public double getRadius() { return radius; }
  45.     // calculate area of Circle
  46.     public double area()
  47.     { return Math.PI * radius * radius; }
  48.     // convert the Circle to a String
  49.     public String toString()
  50.     {
  51.         return "Center = " + super.toString() +
  52.                 "; Radius = " + radius;
  53.     }
  54. }
  55.  
  56. // main.java
  57. import java.text.DecimalFormat;
  58. import javax.swing.JOptionPane;
  59. import newpackage1.Circle;
  60. public class Main {
  61.     public static void main(String[] args) {
  62.         Circle c = new Circle(2, 88, 110);
  63.         DecimalFormat precision1 = new DecimalFormat("0.00");
  64.         String output;
  65.         output = "X coordinate is " + c.getX() +
  66.                 "/nY coordinate is " + c.getY() +
  67.                 "/nRadius is " + c.getRadius();
  68.         c.setRadius(5.5);
  69.         c.setPoint(2, 9);
  70.         output = "/n/nThe new location and radius of c are/n" + c +
  71.                 "/nArea is " + precision1.format(c.area());
  72.         JOptionPane.showMessageDialog(null, output,
  73.                 "Demonstrating Class Circle",
  74.                 JOptionPane.INFORMATION_MESSAGE);
  75.         System.exit(0);
  76.     }
  77. }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值