Basic Terminologies

Java is an Object-Oriented Language. As a language that has the Object Oriented feature, Java supports the its fundamental concepts like classes, objects, inheritance, abstraction, polymorphism etc.

A class is an abstract definition of something that has attributes (sometimes called properties or states) and actions (capabilities or methods). An object is a specific instance of a class that has its own state separate from any other object instance.


Inheritance is one of the fundamental mechanisms for code reuse in OOP. It allows new classes to be derived from an existing class. The new class (also called subclass, subtype, derived class, child class) can inherit members from the old class (also called superclass, supertype, base class, parent class). The subclass can add new behavior and properties and, under certain circumstances, modify its inherited behavior.
In Java, implementation inheritance is achieved by extending classes (i.e., adding new fields and methods) and modifying inherited members. private, overridden, and hidden members of the superclass are not inherited. If no extends clause is specified in the header of a class declaration, the class implicitly inherits class behavior. Classes lower down in the hierarchy are more specialized, as they customize the inherited from the java.lang.Object class.Classes higher up in the hierarchy are more generalized, as they abstract the behavior by additional properties and behavior.
Multiple inheritance is a useful way to create classes that combine aspects of two disparate class hierarchies, the problem with multiple inheritance is that it can lead to ambiguity. Therefore, Java doesnt support multiple inheritance with respect to classes.

In the context of Java, the supertypesubtype relationship implies that the reference value of a subtype object can be assigned to a supertype reference, because a subtype object can be substituted for a supertype object. This assignment involves a widening reference conversion. For example if Animal is the parent class and Dog is a child class the:

Animal animal = new Dog() //widening of reference

Whenever child reference is in parent reference then we cannot run child function . Only parent can be run. Because this is checked in compile time. If a parent class reference variable holds the child class object reference, then at runtime, the JVM will check whether the parent function is overridden in child. If it is overridden then the overridden version of that function will be called, otherwise parent function will be called.  – Dynamic binding

Instance method in the Child class cannot overrride the static method in the parent class, it will give Compile time error. Even if child class has a static method with completely same signature that of a parent class, and we have parent reference and child object, still the parent class static function will be called, that is there is no dynamic binding in case of static because they are not part of the object.

Method overloading deals with the notion of having two or more methods(functions) in the same class with the same name but different arguments.

While Method overriding means having two methods with the same arguments, but different implementation. One of them would exist in the Parent class (Base Class) while another will be in the derived class(Child Class).

Comparison Criteria Overriding Overloading
Method Name Must be same Must be same
Argument list Must be same Must be different
Return Type Can be of same type or a covariant type.
The covariant type refers only to reference
types and not primitive type
Can be different
Throws clause Can narrow Exception thrown.
Must not throw new checked Exception
Can be different
Accessibility Can make it less rrestrictive, but not more
Can be different
Declaration context A method can only be overrriden in the subclass A method can be overloeaded in the same
class or the subclass
Method call resolution The type of the object referenced at runtime
determines which methos is selected for
At compile time, the declared type of the
reference is used to determine which method
will be executed.

Inheritance defines the relationship is-a (also called the superclass–subclass relationship) between a superclass and its subclasses. Aggregation defines the relationship has-a (also called the whole–part relationship) between an instance of a class and its constituents (also called parts). Aggregation comprises the usage of objects.

A subclass cannot override fields of the superclass, but it can hide them. The subclass can define fields with the same name as in the superclass.

The keyword super can also be used in nonstatic code (e.g., in the body of an instance method), but only in a subclass, to access fields and invoke methods from the superclass.

Constructor are made to initialize the object. It tells new operator for whom to reserve memory and how much. Its a especial member function of a class. We cannot call this explicitly. It does not have return type and name is same as that of class. Java provides a default constructor if we have not provided one. We can use both default and parametrized constructor in the same class. First preference goes to the local variable. Every function, every construct, object members can only be
accessed by this.

The this() Constructor Call
Constructors cannot be inherited or overridden. They can be overloaded, but only in the same class. When we use constructor chaining using this operator, it must be the first line in any constructor (even sysout not allowed). The this() call can be followed by any other relevant code. This restriction is due to Java’s handling of constructor invocation in the superclass when an object of the subclass is created.

The super() Constructor Call
A super() call in the constructor of a subclass will result in the execution of the relevant constructor from the superclass, based on the signature of the call. A constructor in a subclass can access the class’s inherited members by their simple names. The keyword super can also be used in a subclass constructor to access inherited members via its superclass. One might be tempted to use the super keyword in a constructor to specify initial values of inherited fields. However, the super() construct provides a better solution to initialize the inherited state.
The super() construct has the same restrictions as the this() construct. This implies that this() and super() calls cannot both occur in the same constructor. If a constructor at the end of a this()chain (which may not be a chain at all if no this() call is invoked) does not have an explicit call to super(), the call super() (without the parameters) is implicitly inserted by the compiler to invoke the default constructor of the superclass.

If a superclass only defines nondefault constructors (i.e. only constructors with parameters), its subclasses cannot rely on the implicit super() call being inserted.
This will be flagged as a compiletime error. The subclasses must then explicitly call a superclass constructor, using the super() construct with the right arguments.

If we print super, it will throw an error which means super is not having any complete reference. But we use is with dot operator. Whenever we create a child class object, parent object is never created, which means parent data members gets memory in child object.

Super and this keyword cannot be used in static functions.

How to access gradparent variavbles. Lets see the following :

package mynotes.shortConcepts;

class GrandParent {
 int x = 10;

class Parent extends GrandParent {
 int x = 20;

class Child extends Parent {
 int x = 30;

void show() {

public class AccessGradParent {
 public static void main(String[] args) {
 (new Child()).show();



Interface in java is declared using keyword interface and it represent a Type like any Class in Java. An interface does not provide any implementation and is, therefore, abstract by definition. This means that it cannot be instantiated.
All variables declared inside interface is implicitly public final variable or constants. which brings a useful case of using Interface for declaring Constants.
The methods in an interface are all implicitly abstract and public by virtue of their definition. Only the modifiers abstract and public are allowed, but these are invariably omitted.
In Java its legal for an interface to extend multiple interface. for example following code will run without any compilation error:

interface SomeInterface extends Serializable, SomeOtherInterface{ }

Note that interface methods cannot be declared static, because they comprise the contract fulfilled by the objects of the class implementing the interface. Interface methods are always implemented as instance methods.

Interfaces with empty bodies can be used as markers to tag classes as having a certain property or
behavior. Such interfaces are also called marker interfaces. Java APIs provide several examples of such marker interfaces: java.lang.Cloneable,, java.util.EventListener.

Abstract class –A class can choose to implement only some of the methods of its interfaces (i.e. give a partial implementation of its interfaces). The class must then be declared as abstract. Static function can never be made abstract. A child class which extend abstract class has to override abstract method (no body). In abstract class we can have combination of both abstract and nonabstract methods.

You can not create instance of abstract class in Java, they are incomplete. Even though, if your abstract class don’t contain any abstract method, you can not create instance of it.
Abstract class can declare and define constructor in Java. Since you can not create instance of abstract class, constructor can only be called during constructor chaining, i.e. when you create instance of concrete implementation class (to initialize common variables).

Abstract class can implement interface by using implements keyword. Since they are abstract, they don’t need to implement all methods.It’s good practice to provide an abstract base class, along with an interface to declare Type. One example of this is java.util.List interface and corresponding java.util.AbstractList abstract class. Since AbstractList implements all common methods, concrete implementations like LinkedList and ArrayList are free from burden of implementing all methods.

It’s not mandatory for an abstract class to have any abstract method. You can make a class abstract in Java, by just using abstract keyword in class declaration.

Abstract class can contain main method, it just another static method and you can execute Abstract class with main method, until you don’t create any instance.

Polymorphism is the ability to provide multiple implementations of an action and to select the correct implementation based on the surrounding context. For example, a class might define two versions of a method with different parameters. Or the same method might be defined both in a parent class and a subclass, the latter overriding the former for instances of the subclass. Method selection may occur when the code is compiled or when the application is run.

Dynamic method lookup is the process of determining which method definition a method signature denotes during runtime, based on the type of the object.

Inheritance Versus Aggregation
Choosing between inheritance and aggregation to model relationships can be a crucial design decision. A good design strategy advocates that inheritance should be used only if the relationship isa is unequivocally maintained throughout the lifetime of the objects involved; otherwise, aggregation is the best choice.

An object has properties and behaviors that are encapsulated inside the object. Encapsulation is achieved through information hiding, by making judicious use of language features provided
for this purpose. Information hiding in Java can be achieved at different levels of granularity:

  • method or block level : Localizing information in a method hides it from the outside.
  • class level : The accessibility of members declared in a class can be controlled through memberaccessibility modifiers. One muchadvocated informationhiding technique is to prevent direct access by clients to data maintained by an object. The fields of the object are private and its contract defines public methods for the services provided by the object. Such tight encapsulation helps to separate the use from the implementation of a class.
  • package level : Classes that belong together can be grouped into relevant packages by using the package statement. Interpackage accessibility of classes can be controlled by class accessibility modifiers

Difference Between Abstraction and Encapsulation
Abstraction focuses on the outside view of an object (i.e. the interface). Encapsulation (information hiding) prevents clients from seeing it’s inside view, where the behavior of the abstraction is implemented. Abstraction solves the problem in the design side while Encapsulation is the Implementation. Encapsulation is the deliverables of Abstraction.

Cohesion is a class measure of how well structured and closely related the functionality is in a class. The objective is to design classes with high cohesion, that perform welldefined and related tasks (also called functional cohesion). The public methods of a highly cohesive class typically implement a single specific task that is related to the purpose of the class. Lack of cohesion in a class means that the purpose of the class is not focused, and unrelated functionality is ending up in the class (also called coincidental cohesion) which will eventually impact the maintainability of the application.

Coupling – Objects need to interact with each other, therefore dependencies between classes are inherent in OO design. However, these dependencies should be minimized in order to achieve loose coupling,which aids in creating extensible applications. One major source of intraclass dependencies is the exposure of implementation details of an object. Such details can be utilized by other objects, and this dependency can impede changes in the implementation, resulting in less extensible applications.

High cohesion and loose coupling help to achieve the main goals of OO design: maintainability, reusability, extensibility, and reliability.

Is there is an alternative to inheritance? YesAssociation

Association is a relationship between two objects. In other words, association defines the multiplicity between objects. You may be aware of one-to-one, one-to-many, many-to-one, many-to-many all these words define an association between objects. Aggregation is a special form of association. Composition is a special form of aggregation. Example: A Student and a Faculty are having an association.

Aggregation is a special case of association. A directional association between objects. When an object ‘has-a’ another object, then you have got an aggregation between them. Direction between them specified which object contains the other object.

Composition is a special case of aggregation. In a more specific manner, a restricted aggregation is called composition. When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition. Example: A class contains students. A student cannot exist without a class. There exists composition between class and students.

Example: A Library contains students and books. Relationship between library and student is aggregation. Relationship between library and book is composition. A student can exist without a library and therefore it is aggregation.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: