C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And classes, building blocks, contain the data and internal logic. They are conceptual units. Classes compose programs.
Complex. Classes are simple when considered alone, but together are complex. Many classes form a model. And with models, we solve hard problems.
Fields, method. This example uses a custom class (Box). This class is stored in a file called Box.java. In Box, we have a constructor, two fields and a method.
Box: In the main method, we create two new instances of the Box class. These exist separately in memory.
Area: We call the area method on each Box instance, box1 and box2. This multiplies the two int fields and returns a number.
Based on: Java 7 Java program that instantiates class, Program.java public class Program { public static void main(String[] args) { // Create box. Box box1 = new Box(2, 3); int area1 = box1.area(); // Create another box. Box box2 = new Box(1, 5); int area2 = box2.area(); // Display areas. System.out.println(area1); System.out.println(area2); } } Class definition, Box.java public class Box { int width; int height; public Box(int width, int height) { // Store arguments as fields. this.width = width; this.height = height; } public int area() { // Return area. return this.width * this.height; } } Output 6 5
Aspects. In the above program, this is found in the two int fields, width and height. It has a means of creation (the constructor). And it provides behavior, in its area() method.
This. With "this," we change what a variable refers to. When an argument and a field (of a class) have the same name, we can use "this" to indicate the field, not the argument.
Here: The name "code" would refer to the argument if it had no "this" before the name. We use a composite name.
Java program that uses this class Cat { int code = 10; public void pet(int code) { // Refers to the code field, not the argument. System.out.println(this.code); } } public class Program { public static void main(String[] args) { Cat cat = new Cat(); cat.pet(5); } } Output 10
This, class references. We can pass the present class to another method with the "this" reference. We forward a class to another method.
Java program that passes this reference class Bird { public String color = "red"; public void call() { // Pass this class instance to another method. Program.analyze(this); } } public class Program { public static void analyze(Bird bird) { // This method does something with a Bird class. System.out.println(bird.color); } public static void main(String[] args) { // Start here. Bird bird = new Bird(); bird.call(); } } Output red
Extends. A class can be based upon another (derived from it). This requires the "extends" keyword in the class declaration. Here class B extends class A.
Note: In main we create a new instance of B but treat it as an A reference. We use a method from A, but the B method is selected.
Call: This method is implemented on both A and B. The most derived B implementation is used when an A reference is acted upon.
Java program that uses derived class class A { public void call() { System.out.println("A.call"); } } class B extends A { public void call() { System.out.println("B.call"); } } public class Program { public static void main(String[] args) { // Reference new B class by A type. A a = new B(); // Invoke B.call from A class instance. a.call(); } } Output B.call
Abstract class. Most classes can be instantiated with "new." An abstract class cannot be. Instead we must create an instance of a derived class.
Abstract: We decorate the Page class with the abstract keyword. We also use an abstract method, one with no body.
Extends: Article and Post are derived from the Page class. They are not abstract. Neither are their open methods.
Here: We create instances of Article and Post, but use them with a Page reference. We call open(). The derived open methods are used.
Java program that uses abstract class abstract class Page { public abstract void open(); } class Article extends Page { public void open() { System.out.println("Article.open"); } } class Post extends Page { public void open() { System.out.println("Post.open"); } } public class Program { public static void main(String[] args) { // We cannot directly create a Page. Page page = new Article(); page.open(); Page page2 = new Post(); page2.open(); } } Output Article.open Post.open
Super. With this keyword, we can reference the "ancestor" or base class of a derived class. Here the class Cat extends the class Animal. I call the scratch() method on Cat.
And: The Cat scratch method itself calls its parent scratch method from the Animal class. We call a "super" class method.
Java program that uses super class Animal { public void scratch() { System.out.println("Animal scratched"); } } class Cat extends Animal { public void scratch() { // Call super-class method. super.scratch(); System.out.println("Cat scratched"); } } public class Program { public static void main(String[] args) { // Create cat and call method. Cat cat = new Cat(); cat.scratch(); } } Output Animal scratched Cat scratched
Constructors. These are used to create new instances of classes. With constructors, we restrict how classes are instantiated. And we validate and initialize data in them.
Objects. The object class is a superclass: every instance in Java has Object as a parent. And in the Objects class, we find utility methods for handling, validating, and comparing objects.
Interface. This is a cross-class abstraction. We isolate features that are used in many classes. Then with an interface reference, we can use those classes in a unified way.
Core unit. Classes are the core unit around which we develop programs. They combine data, in the form of fields, and behavior, in methods. And with constructors we instantiate them.