e. Interface and abstract
Abstract classes: An abstract class
is declared using "abstract" keyword. It allows
putting all common method names in one class without writing
the actual implementation code. An abstract class may or
may not have abstract methods. It cannot be instantiated,
but can be extended into sub-classes.
Abstract method: An abstract method
is a method that is declared without an implementation (without
braces, and followed by a semicolon)
Interfaces: An interface is an reference
type which is similar to a class. It can contain only constants,
methods signatures (there are no method boundaries) and
nested types. Interfaces cannot be instantiated. They can
be implemented by classes or extended by other interfaces.
Difference between Abstract classes and Interfaces
- Abstract classes can contain fields that are not static
and final and they can contain implemented methods, But
- Variables declared in interface are by default final.
An abstract class may contain non-final variables
- Members of interface are public by default. An abstract
class can have private, protected or public members
- Java interface should be implemented using keyword "implements";
A Java abstract class should be extended using keyword "extends".
Object-oriented programming allows classes to inherit commonly
used state and behavior from other classes. Classes can be derived
from other classes, thereby inheriting fields and methods from
A class that is derived from another class is called a subclass
(also a derived class, extended class, or child class). The
class from which the subclass is derived is called a super class
(also a base class or a parent class). When you want to create
a new class and there is already a class that includes some
of the code that you want, you can derive your new class from
the existing class. In doing this, you can reuse the fields
and methods of the existing class without having to write them
Types of inheritance:
- Simple Inheritance: When a subclass is derived simply
from it's parent class then this mechanism is known as simple
inheritance. In case of simple inheritance there is only
a sub class and it's parent class. It is also called single
inheritance or one level inheritance.
- Multilevel Inheritance: When a subclass is derived from
a derived class then this mechanism is known as the multilevel
inheritance. The derived class is called the subclass or
child class for it's parent class and this parent class
works as the child class for it's just above (parent) class.
Multilevel inheritance can go up to any number of levels.
- Multiple Inheritance: The mechanism of inheriting the
features of more than one base class into a single class
is known as multiple inheritances. Java does not support
multiple inheritances but the multiple inheritances can
be achieved by using the interface.
g. Inner classes
An inner class is a class declared within another class.
The four kinds of inner class are:
- Member class: A member class is defined at the top level
of the class, along with fields and methods. It may have
the same access modifiers as variables (public, protected,
package, static, final), and is accessed in much the same
way as variables of that class.
- Static member class: A static member class is defined
like a member class, but with the keyword static. Despite
its position inside another class, a static member class
is actually an "outer" class--it has no special access to
names in its containing class. To refer to the static inner
class from a class outside the containing class, use the
syntax OuterClassName.InnerClassName. A static member class
may contain static fields and methods.
- Local inner class: A local inner class is defined within
a method, and the usual scope rules apply to it. It is only
accessible within that method; therefore access restrictions
(public, protected, package) do not apply. However, because
objects (and their methods) created from this class may
persist after the method returns, a local inner class may
not refer to parameters or non-final local variables of
- Anonymous inner class: An anonymous inner class is one
that is declared and used to create one object (typically
as a parameter to a method), all within a single statement.
The anonymous inner class may either extend a class or implement
An Enums is a class with a fixed number of instances that
are defined within the class itself. Enums are used where there
is a small, unchanging, predefined set of values, such as the
days of the week.
Advantages over using small integers:
- They provide better type safety
- Better readability
- They can be read in, printed out, and compared with
== (as well as equals).
Every Enums extends and inherits from java.lang.Enum. All
constructors declared within an Enums are automatically private.
Since Enums are classes, they can have methods, as well as constructors
Every Enums class creates a new namespace, so that the same
name may be used as a value in different Enums classes.