Body:
Java Access rights and JavaObjects. The role of the final keyword in Java and Java Abstract Classes.
Let’s start today’s sharing
The role of the final keyword in Java
- A final modifier field, which must be initialized when defined and cannot be modified, is used with static in uppercase and is called a final constant
- Final modifier method, cannot be overridden (overridden)
- Final modifies local variables and cannot be modified once initialized. Note that final modifies a reference type variable, meaning that the variable can no longer point to another object and its field value can be modified
- Final decorates method parameters. The value of the final parameter cannot be modified in the method body
package com.bjpowernode.chapter01.demo04.p1; /** * 1) The class is final and cannot be inherited. @author Administrator * */ public final class Father {}Copy the code
package com.bjpowernode.chapter01.demo04.p2; /** * 2) Final fields must be initialized, either at definition or ina constructor. Final fields cannot be reassigned. * @author Administrator * */ public class Father2 {int xx; int yy = 20; final int oo = 456; // Final field displays initialization of final int zz; // Final field initializes in constructor final static double PI = 3.14; // Final constant public Father2() {zz = 121; } public void mm() { xx = 123; yy = 202; // zz = 789; //he final field Father2.zz cannot be assigned // oo = 369; // Final fields cannot be reassigned}}Copy the code
package com.bjpowernode.chapter01.demo04.p3; /** * 3) Final modifier, cannot be overridden by subclasses * note: subclasses can inherit from final methods, * @author Administrator * */ public class Father3 {public void m1(int xx) { System.out.println(" instance method of parent class, subclass overridden "); } public final void m2(int x, int y) {system.out.println (" final "); }}Copy the code
package com.bjpowernode.chapter01.demo04.p4; import com.bjpowernode.chapter01.demo03.Book; /** * 4) Final decorates local variables * once initialization is complete, Public class Test {public static void main(String[] args) {public static void main(String[] args) { x = 10; int y = 20; //final final int z; z = 30; final int t = 40; // We can reassign the local variable x = 101; y = 202; // z = 303; Final Book book1 = new Book // The final keyword is used to modify the book1 variable. It cannot reassign the value of book1 new Book book1.name = } }Copy the code
package com.bjpowernode.chapter01.demo04.p5; * @author Administrator ** / public class Test {public static void main(String[]) args) { m1(10, 20); } public static void m1(int x, final int y) {x = 101; // y = 202; // Final parameters cannot be assigned}}Copy the code
Java classes
When an operation of a class cannot be implemented concretely, the operation can be defined as an abstract method.
Abstract methods use methods modified by abstract, with only the declarative part of the method and no method body.
package com.wkcto.chapter01.abstracttest.demo01; /** * This class has operations on the area and circumference of a plane graph ** 1) when an operation of a class is not implementable, the operation can be defined as abstract method * 2) when the method is abstract. Public abstract class * @author * */ public Abstract class Graphics2d {// Define the abstract method, find the area public abstract double getArea(); Public abstract double getPerimeter(); }Copy the code
package com.wkcto.chapter01.abstracttest.demo01; * @author ** / Public Class Rectangle extends Graphics2d {int width; int height; public Rectangle(int width, int height) { super(); this.width = width; this.height = height; } @Override public double getArea() { return width*height; } @Override public double getPerimeter() { return 2*(width+height); }}Copy the code
package com.wkcto.chapter01.abstracttest.demo01; /** * test class, Public class Test {public static void main(String[] args) {public static void main(String[] args) Rectangle rect = new Rectangle(10, 5); // The method's parameter graphics2d is a superclass reference, passing the subclass object showInfo(rect) when calling the method; Circle c1 = new Circle(10); showInfo(c1); } public static void showInfo(Graphics2d) {public static void showInfo(Graphics2d) { System.out.println(" graphic information ------------------"); System.out.println(" area :" + graphics2d.getarea ()); System.out.println(" perimeter :" + graphics2d.getperimeter ()); }}Copy the code
package com.wkcto.chapter01.abstracttest.demo01; /** * Define triangle class * if subclass inherits abstract class, * @author * */ public Abstract class Triangle extends Graphics2d {int a; int b; int c; @Override public double getPerimeter() { return a+b+c; }}Copy the code
package com.wkcto.chapter01.abstracttest.demo01; /** * Test abstract class features: Classes that contain abstract methods must be defined as abstract classes. However, they do not have to contain abstract methods. Sometimes, existing classes can be abstracted to form higher classes (more abstract classes), such as food classes, animal classes, etc. They cannot be instantiated, in which case they can be defined as abstract classes * 3) Abstract classes are reference data types that can define variables, but abstract classes cannot instantiate objects. * Abstract classes define references that can assign subclass objects * 4) Abstract classes must contain constructors, * @author * */ public class Test02 {public static void class Test02 {public static void class Test02 {public static void class Test02 Main (String[] args) {// Define a variable Graphics2d Graphics2d with an abstract class; // graphics2d = new Graphics2d();; //Cannot instantiate the type Graphics2d Rectangle rectangle = new Rectangle(10, 5); // Assign the subclass object graphics2d = Rectangle; graphics2d = new Rectangle(5, 2); }}Copy the code
Exercise: Pets act cute when their owners feed them
package com.wkcto.chapter01.abstracttest.demo02; Public void sellMeng(); @author ** / public void sellMeng(); }Copy the code
package com.wkcto.chapter01.abstracttest.demo02; @author ** / public class extends Pet {@override public void sellMeng() { System.out.println(" woof woof ~~~~"); }}Copy the code
package com.wkcto.chapter01.abstracttest.demo02; Public void feed(Pet Pet) {// Public void feed(Pet Pet) {// public void feed(Pet Pet) {// Pet Pet.sellmeng (); }}Copy the code
package com.wkcto.chapter01.abstracttest.demo02; Public class Test {public static void main(String[] args) {Master xiaoxi = new Master(); Dog dog = new Dog(); xiaoxi.feed(dog); Cat cat = new Cat(); xiaoxi.feed(cat); }}Copy the code
You can practice it yourself:
Angry birds, red (red color of the bird) can fly normally, blue ice (blue color of the bird) can change three, wasp (yellow color of the bird) can accelerate the flight, define a slingshot class, there is a catapult method, can put the bird to shoot out, the bird is flying.
-
Above is the Role of the Final keyword in Java and Java Abstract Classes. Java interfaces and the relationship between Java classes.
-
Also welcome everybody exchange discussion, if this article has incorrect place, hope everybody many forgive.
-
Your support is my biggest motivation, if you help out, give me a thumbs up