Abstract classes in Java

Source: Internet
Author: User


A class with abstract methods, which is an abstract method, that is, the parent class knows only that its subclasses should contain the method, but cannot know how the subclasses implement these methods
Abstract class constraints require subclasses to implement certain methods, but do not care about implementation details
Abstract classes can contain normal methods (still virtual methods in Java), or they can have no abstract methods
Abstract classes cannot be used to instantiate objects, but you can define reference variables to use to specify child class objects

Reference code:

Abstract class shape

 Package Com.hao.practice;  Public Abstract class Shape {    publicabstractfloat  perimeter ();      Public Abstract float Area ();}
View Code

Sub-class Circle

 PackageCom.hao.practice; Public classCircleextendsShape {Static Final floatPI = 3.14f; floatr = 0.0f; /** Default constructor*/     PublicCircle () {Super(); }    /** with parameter constructor * @param r*/     PublicCircle (floatr) {Super();  This. R =R; }    /** (Non-Javadoc) * @see com.hao.practice.shape#perimeter ()*/@Override Public floatperimeter () {//TODO Auto-generated method stubs        return2*pi*R; } @Override Public floatArea () {//TODO Auto-generated method stubs        returnpi*r*R; }}
View Code

Sub-class Rectangle

 PackageCom.hao.practice; Public classRectangleextendsShape {floatA = 0.0f; floatb = 0.0f; /*** Default constructor*/     PublicRectangle () {Super(); }    /*** Copy constructor *@paramA *@paramb*/     PublicRectangle (floatAfloatb) {Super();  This. A =A;  This. B =b; } @Override Public floatperimeter () {//TODO Auto-generated method stubs        return2*a*b; } @Override Public floatArea () {//TODO Auto-generated method stubs        returnA *b; }}
View Code

Abstract classes in 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.