This article illustrates the concepts of inheritance vs. composition in Java. It first shows an example of inheritance, and then shows how to improve the inheritance design by using composition. How to choose between them is summarized at the end.
Classes & Interfaces
Top 9 questions about Java Maps
In general, Map is a data structure consisting of a set of key-value pairs, and each key can only appears once in the map. This post summarizes Top 9 FAQ of how to use Java Map and its implemented classes. For sake of simplicity, I will use generics in examples. Therefore, I will just write Map
instead of specific Map
. But you can always assume that both the K and V are comparable, which means K extends Comparable
and V extends Comparable
.
Top 10 questions about Java Collections
The following are the most popular questions of Java collections asked and discussed on Stackoverflow. Before you look at those questions, it’s a good idea to see the class hierarchy diagram. 1. When to use LinkedList over ArrayList? ArrayList is essentially an array. Its elements can be accessed directly by index. But if the array … Read more
What Is Inner Interface in Java?
What is Inner Interface in Java?
Inner interface is also called nested interface, which means declare an interface inside of another interface. For example, the Entry interface is declared in the Map interface.
public interface Map { interface Entry{ int getKey(); } void clear(); } |
Constructors of Sub and Super Classes in Java?
This post summarizes a commonly asked question about Java constructors.
Why Field Can’t Be Overridden?
This article shows the basic object oriented concept in Java – Field Hiding. 1. Can Field Be Overridden in Java? Let’s first take a look at the following example which creates two Sub objects. One is assigned to a Sub reference, the other is assigned to a Super reference. package oo; class Super { … Read more
How Static Type Checking Works in Java?
From Wiki: Static type-checking is the process of verifying the type safety of a program based on analysis of a program’s source code. Dynamic type-checking is the process of verifying the type safety of a program at runtime Java uses static type checking to analyze the program during compile-time to prove the absence of type … Read more
When to use private constructors in Java?
If a method is private, it means that it can not be accessed from any class other than itself. This is the access control mechanism provided by Java. When it is used appropriately, it can produce security and functionality. Constructors, like regular methods, can also be declared as private. You may wonder why we need … Read more
What is Instance Initializer in Java?
In this post, I will illustrate what are instance variable initializer, instance initializer, static initializer, and how the instance initializer works in Java. 1. Execution Order Look at the following class, do you know which one gets executed first? public class Foo { //instance variable initializer String s = "abc"; //constructor public Foo() … Read more
4 types of Java inner classes
There are 4 different types of inner classes in Java. This post illustrates them by using 4 simple examples. 1. Static Nested Classes class Outer { static class Inner { void go() { System.out.println("Inner class reference is: " + this); } } } public class Test { public static void main(String[] args) { Outer.Inner … Read more