Simple Inheritance Example: Java

Source: Internet
Author: User

Generic class to inherit the circles and rectangles.

Package circle;public class geometric {private string color= "White";p rivate  boolean filled;private java.util.date datecreated;public geometric () {dateCreated=new  java.util.date ();} Public geometric (string color,boolean filled) {datecreated=new java.util.date (); this.filled =filled;this.color=color;} Public java.util.date getdatecreated () {return datecreated;} Public string getcolor () {return color;} Public void setcolor (String color) {this.color=color;} Public boolean isfilled () {return filled;} Public void setfilled (boolean filled) {this.filled=filled;} Public string tostring () {return  "Created on" +datecreated+ "\ncolor:" +color+ "and filled   "+filled;}} package circle;import java.util.*;p Ublic class circle extends geometric{private  double radius;public circle () {}PUBLIC CIRCLE (Double radius) {This.radius=radius;} Public circle (double radius,string color,boolean filled) {This.radius=radius;setcolor (color ); setfilled (filled);} Public double getradius () {Return radius;} Public void setradius (Double radius) {This.radius=radius;} Public double getarea () {Return math.pi*radius*radius;}} Package circle;public class rectangle extends geometric {private double  width;private double height;public rectangle () {}public rectangle (Double width, Double height) {this.width=width;this.height=height;} Public rectangle (double width,double height,string color,boolean filled) {this.width= Width;this.height=height;setcolor (color); setfilled (filled);} Public double getwidth () {return width;} Public void setwidth (double width) {this.width=width;} Public double getheight () {return height;} Public&nbsP;void setheight (double height) {this.height=height;} Public double getarea () {return width*height;} Public double getperimeter () {return 2* (width+height);}} Package circle;public class main {public static void main (String[]  args)  {/**geometric g1=new geometric (); g1.setfilled (true); Geometric g2=new geometric ("Black", false); System.out.print (G1.tostring ()); System.out.print (G2.tostring ()); */circle c1=new circle (); C1.setradius (1.0); System.out.println (C1.getarea ()); Circle c2=new circle (2.0, "Red", true); System.out.println (C2.getarea ()); Rectangle r1=new rectangle (); System.out.println (R1.getarea ()); Rectangle r2=new rectangle (1.0,2.0); System.out.println (R2.getarea ());}}

When there are two packages in Java, if you use other packages in a package class, you can import the. Package name. class name; easy to use.


Overloading means that you can define multiple methods with the same name, but these methods have different signatures.

The overriding method means providing a completely new implementation for the methods in the subclass. Specific examples are as follows:

Method P of Class A overrides the same method defined in class B. public class Test{public static void Main (string[] args) {a a=new a (); A.P () A.P (10.0);}} Class B{public void P (double i) {System.out.println (i*2);}} Class A extends B{public void P (double i) {System.out.println (i);}}
Class B has two overloaded methods P (double i) and P (int i), and the P (double i) Method of Class B is inherited. public class Test{public static void Main (string[] args) {a a=new a (); A.P () A.P (10.0);}} Class B{public void P (double i) {System.out.println (i*2);}} Class A extends b{public void p (int i) {System.out.println (i);}}


This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1880621

Simple Inheritance Example: Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.