Friday 18 August 2017

Inheritance in Java Interview


  • Inheritance is one of the corner stones of object oriented programming because it allows the creation of hierarchical classifications. Using inheritance you can create a general class that defines traits common to a set of related items.
  • This class can then be inherited by other specific classes each adding those things that are unique to it. A class that is inherited is called a “Super Class”. The class that does the inheriting is called a “Sub Class”. Hence a sub class is a specialized version of a super class.
  • To inherit a class you incorporate the definition of one class into another by using “extends” keyword.
  • You can only specify one super class for any sub class that you create. Java does not support the inheritance of multiple super classes into a single sub class.
  • Although a sub class includes all of the members of its super class it cannot access those members of the super class that have been declared as private.
  • There will be times when you will want to create a super class that keeps the details of its implementation to itself (that keeps its data members private). In this case there would be no way for a sub class to directly access or initialize these variables on its own. Since encapsulation is a primary attribute of OOP it is not surprising that Java provides a solution to this problem. Whenever a sub class needs to refer to its immediate super class it can do so by use of the keyword “super”.
  • Super has two general forms. The first calls the super class constructor. A subclass can call a constructor defined by its super class by use of the following form: super(parameter-list);
  • The second form of super acts somewhat like “this”, except that is always refers to the super class of the sub class in which it is used. This usage has the following general form: super.member. Here member can be either a method or an instance variable. This second form of super is applicable to situations in which member names of a sub class hide members by the same name in the super class.

METHOD OVERRIDING
·         In a class hierarchy when a method in a sub class has the same name and type signature as a method in its super class then the method in the sub class is said to override the method in the super class. When an overriden method is called from within a sub class it will always refer to the version of that method defined by the sub class. The version of the method defined by the super class will be hidden.
·         Method overriding occurs only when the names and the type signatures of the two methods are identical. If they are not then the two methods are simply overloaded.
·         Method overriding forms the basis for one of Java’s most powerful concepts: dynamic method dispatch. Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time rather than compile time. Dynamic method dispatch is important because this is how Java implements run time polymorphism.
·         A super class reference variable can refer to a sub class object. Java uses this fact to resolve calls to overriden methods at run time. When an overridden method is called through a super class reference, Java determines which version of that method to execute based upon the type of the object being referred to at the time the call occurs. Thus this determination is done at run time.
·         Overriden methods allow Java to support run time polymorphism. Polymorphism is essential to object oriented programming for one reason: it allows a general class to specify methods that will be common to all of its derivatives while allowing sub classes to define the specific implementation of some or all of those methods.




USING ABSTRACT CLASSES
·         There are situations in which you will want to define a super class that declares the structure of a given abstraction without providing a complete implementation of every method. You want to create a super class that only defines a generalized form that will be shared by all of its sub classes leaving it to each sub class to fill in the details.
·         One way this situation can occur is when a super class is unable to create a meaningful implementation for a method. Java’s solution to this problem is the “abstract method”.
·         You can require that certain methods be overridden by sub classes by specifying the abstract type modifier. These methods are sometimes referred to as sub classer responsibility because they have no implementation specified in the super class. Thus a sub class must override them. to declare an abstract method use the general form:                      abstract type name(parameter – list);
·         Any class that contains one or more abstract methods must also be declared abstract. There can be no objects of an abstract class.
·         It is not possible to instantiate objects from abstract classes but they can be used to create object references because Java’s approach to run time polymorphism is implemented through the use of super class references. Thus is must be possible to create a reference to an abstract class so that is can be used to point to a sub class object.

No comments:

Post a Comment