Did you know – Advanced Class Design

Can you stay with me till the end of the post? 🙂

Did you know that:

  • The return type of an overridden method should be the same or more restrictive aka covariant return types
  • abstract methods are not allowed in a class that is not abstract
  • all methods in an interface are public
  • the interface itself does not have to be public
  • the ClassCastException is a runtime exception
  • a subclass which has the same static method as its superclass is not overriding this method
  • an overridden method may be declared final
  • it cannot throw a checked exception that is not declared in the superclass
  • virtual method invocation relates to java looking for an overridden method instead of using the one of the reference variable
  • virtual METHOD invocation does not relate to instance variables
  • if a.equals(b) is true, than a.hashCode() should return the same value as b.hashCode()
  • if a.equals(b) is false, than a.hashCode() might still be the same as b.hashCode()
  • a.equals(null) should always return false
  • String overrides the equals method bet StringBuilder uses the one it inherits from Object
    StringBuilder a= new StringBuilder("HELLO JAVA");
    StringBuilder b= new StringBuilder("HELLO JAVA");
    if (a.toString().equals(b.toString()))
        System.out.println("SBs are equal");
        System.out.println("SBs are not equal");
    //output : SBs are not equal
  • the Apache Commons Lang library provides some useful methods for creating toString() and equals() overrides
  • you cannot extend an enum
  • you cannot compare an int with an enum
  • enums can have constructors, methods and even abstract methods
  • but that constructor cannot be public
  • that it is time for another example:
    public class HelloWorld {
        public static void main(String []args){
            Animal c = Animal.CAT;
            Animal m = Animal.MOUSE;
        enum Animal {
                public void makeNoise() {
                public void makeNoise(){
            private final String sound;
            Animal(String sound) { this.sound = sound; }
            public String getSound() { return sound; }
            public abstract void makeNoise();
  • the method ordinal() of Enum returns the ordinal of this enumeration constant
  • the method name() of Enum returns the name of that enum constant
  • a member inner class is a non-static nested class
  • member inner classes  cannot declare static fields or static methods
  • they can also access instance variables of the outer class
  • you need an object of the outer class to construct an object of a member inner class
  • compiling an outer class with an inner class wil create an Outer.class file and a Outer$Inner.class file
  • a local inner class is a inner class defined in the body of a method
  • local inner classes do not have access modifiers
  • an anonymous inner class is an inner class with no name and for which only a single object is created.
  • anonymous inner classes can even instantiate an abstract class
  • local inner classes can access local variables if they are effectively final
  • a variable is effectively final if the code would still compile if the final keyword is inserted in front of the variable
  • this means the variable is declared and defined only ones
  • a static nested class is actually not an inner class
  • it is a static class defined at the member access level
  • it can declare static variables and methods
  • one can instantiate an object of a static inner class without the need of an object of an outer class
  • inner classes can implement multiple interfaces and extend any class, except for the anonymous inner class
  • this one can have only one superclass or interface
  • we can only create a Baby if we have a Mother object: (unless the Baby ‘d be static, but hey, you don’t wanna share your baby with all other Moms)
    public class Mother {
        class Baby {}
        public static void main(String... args){
            Mother.Baby baby = new Mother().new Baby();

Leave a Reply

Your email address will not be published.