Tuesday, 4 September 2007

Abstract Class, Nested Class

Abstract class 

An abstract class is a partially implemented class, so it can not be instantiated. The purpose of the abstract class is to set a place to put common attributes and method pulled out from a category of classes. 

As a creator, you may require some behaviours that derivatives must carry them out. An abstract method is a method signature without implementation, it enforces all non-abstract sub-classes to implement it in their own unique ways. Abstract methods are must be implemented by the sub-classes. If a subclass doesn't want to implement it, it must re-declare this abstract method again in its class body. Only an abstract class can declare abstract methods. Any class contains an abstract method should be declared as an abstract class. 

An abstract class can also have constructors, although an abstract class cannot be instantiated. The purpose of constructors enforces the non-abstract subclasses to initialize as creator designed. The decorator pattern is a good example to explain this, where an abstract decorator enforces the concreate decorator to initialize the association relationship with a decorated instance.  A non-abstract subclass must invoke at least one of a constructor from the abstract superclass.  Otherwise, it gives a compilation error.

An abstract class can implement interfaces, but it doesn't need to implement them. However, its subclasses have to implement them, if the abstract class doesn't implement them yet. 

An abstract class can be extended from a concrete class, for instance, object class is a concrete class, but any abstract must is an extension from it. 

A concrete method could be overridden to be abstract. 

All abstract methods defined in the Abstract classes are enforced to be carried out in the sub-classes. 

Although abstract class cannot be instantiated, it can be used as a data type. An abstract type reference variable can be assigned by all of its non-abstract subclass instances. 

What difference between an abstract class and a concrete class? 

abstract do almost all concrete class can do, but only two differences
  1. an abstract class cannot be instantiated, but a concrete class can.
  2. an abstract method can be defined in abstract class alone, but not in a concrete class. 

Nested Class

A class that is defined within another class, it is a nested class. 

A nested class defined as static is referred to as a static nested class. Nested classes that are not declared as static are referred to as inner classes. 

A nested class can also be defined within methods.  It is referred to as a method local inner class. 
Anonymous inner class is a local class without a name. 












No comments:

Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...