In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces you how to understand the abstract methods and abstract classes of Java, the content is very detailed, interested friends can refer to, hope to be helpful to you.
A little bit of eye contact
An abstract method is a method that has only a method signature and no method implementation.
Abstract methods and abstract classes must be defined using abstract modifiers. Classes with abstract methods can only be defined as abstract classes, and there can be no abstract methods in the abstract class.
Rules for abstract classes and abstract methods:
Abstract classes must be decorated with abstract modifiers, abstract methods must be decorated with abstract modifiers, and abstract methods cannot have method bodies. Abstract classes are not necessarily instantiated. An abstract class cannot create an instance even if it does not contain abstract methods. Abstract classes can contain five components: member variables, methods (common methods and abstract methods), constructors, initialization blocks, and inner classes (interfaces, enumerations). The constructor of an abstract class cannot be used to create an instance, but is mainly used to be called by its subclass. A class that contains an abstract method (including directly defining an abstract method; or inheriting an abstract parent class without fully implementing the abstract method contained in the parent class; or implementing an interface, but not fully implementing the abstract methods contained in the interface) can only be defined as an abstract class.
Two code
1 abstract class
Public abstract class Shape {{System.out.println ("execute the initialization block of Shape...");} private String color; / / define an abstract method public abstract double calPerimeter () to calculate the perimeter; / / define an abstract method public abstract String getType () that returns the shape. / / defines the constructor of Shape, which is not used to create Shape objects, but is used by quilt subclasses to call public Shape () {} public Shape (String color) {System.out.println ("constructor that executes Shape..."); this.color = color;} / / omits the setter of color and the getter method public void setColor (String color) {this.color = color;} public String getColor () {return this.color;}}
2 Triangle class
Public class Triangle extends Shape {/ / defines three sides of a triangle private double a; private double b; private double c; public Triangle (String color, double a, double b, double c) {super (color); this.setSides (a, b, c);} public void setSides (double a, double b, double c) {if (a > = b + c | b > = a + c | | c > = a + b) {System.out.println ("the sum of two sides of a triangle must be greater than the third side"); return } this.a = a; this.b = b; this.c = c;} / / override the abstract method public double calPerimeter () {return a + b + c;} / override the abstract method public String getType () {return "triangle";}} of the return shape of the Shape class
3 Circle class
Public class Circle extends Shape {private double radius; public Circle (String color, double radius) {super (color); this.radius = radius;} public void setRadius (double radius) {this.radius = radius;} / / override the abstract method public double calPerimeter () {return 2 * Math.PI * radius;} / / override the abstract method public String getType () {return getColor () + "circle" of the return shape of the Shape class } public static void main (String [] args) {Shape S1 = new Triangle ("black", 3,4,5); Shape S2 = new Circle ("yellow", 3); System.out.println (s1.getType ()); System.out.println (s1.calPerimeter ()); System.out.println (s2.getType ()); System.out.println (s2.calPerimeter ());}
Three operation
Execute the initialization block of Shape. The constructor that executes the Shape. Execute the initialization block of Shape. The constructor that executes the Shape. Triangle 12.0 yellow circle 18.84955592153876
Four attentions
Abstract can not be used to modify member variables, can not be used to modify local variables, that is, there are no abstract variables, no abstract member variables and so on; abstract can not be used to modify constructors, there is no abstract constructor, the constructors defined in abstract classes can only be ordinary constructors.
Static and abstract cannot decorate a method at the same time, but they can decorate inner classes at the same time.
A method modified by the abstract keyword must be overridden by a subclass to make sense, otherwise the method will never have a method body, so the abstract method cannot be defined as private access, that is, private and abstract cannot modify the method at the same time.
On how to understand Java abstract methods and abstract classes to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.