1. Complete structure of a Java class
5. Implementing super interfaces
<class modifiers> class className extends <super_class> implements <super_interface> { Field Declarations Static Initializers Instance Initializers Constructors Method Declarations }Below is an example class that follows the above syntax. No worries if you don’t understand, as I’ll explain further below.
public class RoundedCornerSquare extends Square implements Shape { /** Field Declarations: **/ private static double cornerRadius; private String fillColor; private String borderColor; /** Static Initializers: **/ static { cornerRadius = 1.58; } /** Instance Initializers: **/ { fillColor = "White"; borderColor = "Black"; } /** Constructors: **/ public RoundedCornerSquare() { this.width = 15; } public RoundedCornerSquare(int width) { this.width = width; } public RoundedCornerSquare(int width, String fillColor) { this.width = width; this.fillColor = fillColor; } /** Method Declarations: **/ public double calculateArea() { return width * width; } public double calculatePerimeter() { return width * 4; } public void printDetails() { System.out.print("Rounded Corner Square[width=" + width); System.out.print(", radius=" + cornerRadius); System.out.print(", fillColor=" + fillColor); System.out.println(", borderColor=" + borderColor + "]"); } public static void main(String[] args) { RoundedCornerSquare square1 = new RoundedCornerSquare(); RoundedCornerSquare square2 = new RoundedCornerSquare(20, "Blue"); square1.printDetails(); square2.printDetails(); } }Code of the Shape interface:
public interface Shape { double calculateArea(); double calculatePerimeter(); }Code of the Square class:
public class Square { protected int width; public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } }Let me explain each part of the class structure in detailed.
public class EnclosingClass { private class MemberClass { } public void test() { MemberClass m = new MemberClass(); } }A protected member class is accessible in the enclosing class and different classes in the same package. Below is an example:
public class EnclosingClass { protected class MemberClass { } }An abstract class means that it is incomplete with abstract methods that has no body (no concrete code). And non-abstract subclasses must provide implementation for the abstract methods. Below is an example:
public abstract class Animal { public void eat() { System.out.println("Eating.."); } public abstract void move(); } public class Bird extends Animal { public void move() { System.out.println("Flying..."); } }You can declare a static class inside another class. Read this article for more details.A final class cannot be extended. Check this article to learn more about final class.And read this article to understand about using strictfp modifier.Recent Java versions allow you to declare a sealed class which specifies exactly its known subtypes. For example:
public sealed class Shape permits Rectangle, Circle, Triangle { }Here, the Shape class permits exactly only 3 subclasses that are Rectangle, Circle and Triangle. These subclasses are known at compile time, and no other classes can extend the Shape class.
public class RoundedCornerSquare extends Square { } public class Square { protected int width; public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } }This means RoundedCornerSquare is a subtype of Square and it inherits the member variable width and methods getWidth() and setWidth().Note that a class can extend only one direct superclass. Read this article to learn more about inheritance in Java.
public class RoundedCornerSquare extends Square implements Shape { }Below is another example:
public class SUV extends Car implements Vehicle, Moveable { }The class SUV implements two interfaces Vehicle and Moveable so it must override methods declared in those interfaces.Learn more: 12 Rules and Examples About Inheritance in Java
public class RoundedCornerSquare extends Square implements Shape { private static double cornerRadius; private String fillColor; private String borderColor; }Here, this class has 3 fields. The first one is a static field (cornerRadius) and the last twos are instance fields. Read my explanation in this article to understand static vs. non-static stuffs in Java.You can declare any number of fields in a class and in any order. However, it’s a good practice to declare fields at the beginning of the class for readability.
public class RoundedCornerSquare extends Square implements Shape { private static double cornerRadius; static { cornerRadius = 1.58; } }In a Java class, you can put any number of static initializers and they are executed in the order specified in the class. Learn more: Java default Initialization of Instance Variables and Initialization Blocks
public class RoundedCornerSquare { private String fillColor; private String borderColor; { fillColor = "White"; borderColor = "Black"; } }An instance initializer is the code between { } block. You can put any number of instance initializers in a class, and they are executed in the order they appear in the class.Read this article to learn more about initialization blocks.
public class RoundedCornerSquare extends Square implements Shape { private static double cornerRadius; private String fillColor; private String borderColor; public RoundedCornerSquare() { this.width = 15; } public RoundedCornerSquare(int width) { this.width = width; } public RoundedCornerSquare(int width, String fillColor) { this.width = width; this.fillColor = fillColor; } }You can see this class has 3 constructors, with each has different number of parameters.Learn more: 9 Rules about Constructors in Java
public class RoundedCornerSquare extends Square implements Shape { public double calculateArea() { return width * width; } public double calculatePerimeter() { return width * 4; } public void printDetails() { System.out.print("Rounded Corner Square[width=" + width); System.out.print(", radius=" + cornerRadius); System.out.print(", fillColor=" + fillColor); System.out.println(", borderColor=" + borderColor + "]"); } public static void main(String[] args) { RoundedCornerSquare square1 = new RoundedCornerSquare(); RoundedCornerSquare square2 = new RoundedCornerSquare(20, "Blue"); square1.printDetails(); square2.printDetails(); } }A method can be static or non-static. You can declare any number of methods in any order in the class. However, it’s a good practice to keep the number of methods and the method body as small as possible.So far, you have learned details about structure of a Java class with code examples. I recommend you read the following articles to learn more.Watch the following video to see the coding in action: Further Reading: