Java Novice Note 19 abstract class

Source: Internet
Author: User

1.Shap

Package Com.yfs.javase;public class Shape/*extends object */{  //default Inherit Object  object method all inherit//Calculate Area method public double Getarea () {//code completely useless System.out.println ("Calculate area"); return 0;}}

2.Circle

Package Com.yfs.javase;public Class Circle extends Shape {private double r;public Circle (double r) {THIS.R = R; SYSTEM.OUT.PRINTLN ("Create circular area"); Public double Getarea () {//override method//system.out.println for parent class ("Calculate circular area ...");//return 3.14 * R * r;//}}

3.Triangle

Package Com.yfs.javase;public class Triangle  extends Shape {private double height;private double width;public Triangle (double height, double width) {this.height = Height;this.width = width; System.out.println ("Create Triangle area"); Public double Getarea () {System.out.println ("Calculate Triangle area ..."), return 1.0/2 * width * height;}

4.Rectangle

Package Com.yfs.javase;public class Rectangle  extends Shape {private double width;private double length;public Rectangle (double width, double length) {this.width = Width;this.length = length; SYSTEM.OUT.PRINTLN ("Create rectangular area"); Public double Getarea () {System.out.println ("Calculate Rectangle area ..."); return width * length;}}

5.shap Test

Package Com.yfs.javase;public class Shapetest {public static void main (string[] args) {shape[] shapes = new Shape[10];
   shape s = new Shape ();//Create this Class object S.getarea ();}}

6. Abstract class

The abstract method class in the package com.yfs.javase;//class must be abstract public abstract class Demo1 {public abstract void method ();//No method body abstract method public A Bstract double method1 (int A, String name);    public abstract double method1 (String name,int a); An abstract class can declare that a normal method subclass can inherit public void Method2 () {System.out.println ("Call METHOD2 () ... ");} Whether the normal method can invoke the abstract method public void Method3 () {method ();//methods that do not call the parent class method to call the subclass object are polymorphic}}abstract class Child extends Demo1 {@Overridepu Blic void Method () {System.out.println ("call () ...");} Class Son extends Child {@Overridepublic double method1 (int A, String name) {//TODO auto-generated method Stubreturn 0;} @Overridepublic Double method1 (String name, int a) {//TODO auto-generated method Stubreturn 0;}} Class Children extends Demo1 {//Subclass inheriting abstract class must override abstract method @overridepublic void Method () {System.out.println ("Subclass overridden Abstract method ...");} @Overridepublic double method1 (int A, String name) {System.out.println ("name =" + name); System.out.println ("a =" + a); return 0;} @Overridepublic Double method1 (String name,int a) {//TODO auto-generated method Stubreturn 0;} public static DEMO1 test (Demo1 D) {return new Children ();}}

7. Abstract class Testing

Package Com.yfs.javase;public class Demo1test {/** * 1. Abstract classes cannot be instantiated * 2. Subclasses must override abstract methods of abstract classes */public static void Main (string[] args {Demo1 d1;//declares reference//creation Object//D1 = new Demo1 ();//abstract class cannot instantiate D1 = new Children ();d 1.method ();d 1.method2 (); Children C1 = new Children (); C1.method2 (); System.out.println ("============================");d 1.method3 () c1.method3 ();d 1.method1 (15, "Shadow Clone");D emo1 d2 = Children.test (C1);D Emo1 c2 = children.test (d1);d 2.method2 (); C2.method2 ();}}

Java Novice Note 19 abstract class

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.