Wednesday, July 13, 2022

Inheritance

Q What is the purpose of this keyword in Java?
  • In Java this keyword refers to current instance of the object.
  • It is useful for differentiating between instance variables and local variables.
  • It can be used to call constructors. Or It can be used to refer to the instance.
    • To call a constructor this statement must be placed at the top of overloaded constructor.
    • For example in a class vehicle 
      • this(“Car”, 4) 
    • Above statement calls an argument constructor from a no args constructor. The no args constructor should have above as first line of code.
  • In case of method overriding, this is used for falling the method of current class.
Q Explain the concept of inheritance?
  • Inheritance is an important concept in object oriented programming. Some objects share certain characteristics and behaviour. By using inheritance, we can put the common behaviour and characteristics in a base class which also known as superclass. And then all the objects with common behaviour inherit from this base class.
  • It is also represented by IS-A relationship.
  • Inheritance promotes, code reuse, method overriding and polymorphism.
  • Child class object carries the body of the parent class when initiated. Moreover there are certain privileges attached to method overloading to the classes related this way. This relationship exists for Code reuse, method overriding and interfacing (through abstract class).
  • Is a relationship can be achieved by using “extends” keyword.
Q Which class in Java is superclass of every other class?
  • Java is an object oriented programming language. In Java, object class is the superclass of every other class.
Q Why Java does not support multiple inheritance?
  • Multiple inheritance means that a class can inherit behaviour from two or more parent classes.
  • The issue with multiple inheritance is that both the payment classes may have different implementation of the same method. So they have different ways of doing the same thing. Now which implementation should the child class choose?
  • This leads to ambiguity in multiple inheritance this is the main reason for Java not supporting multiple inheritance in implementation.
  • Let’s see you have a class TV and another class Steriliser. Both have method switch on but only TV has switch off method. If your class in adults from both these classes then we have an issue that we can switch on both parents but switch off will only switch off TV.
  • We can implement multiple interfaces in Java.
Q What is the difference between abstract class and interface?
  • In interface we do not need to define abstract keyword for methods. All methods are abstract by default.
  • We can implement multiple interfaces but can extend only one abstract class.
  • All data members of interface are static and final. We need to instantiate it with a value. Some values are applied to every object of a class.Data members in abstract class or not same across all the objects
  • If we have a set of closely related classes That have same functionality and same types of fields available we can use abstract class. If we have a set of unrelated classes that we want to achieve certain functionality then we use interfaces.
Q In OOPS what is meant by composition?
  • Composition is also known as “has-a” relationship. In composition, “has-a” relation relates two classes. Example class car has a steering wheel.
  • If a class holds the instance of another class, then it is called composition.
Q What is association?
  • Association is relation between two separate classes which establishes through their objects. Association can be one-to-one, one-to-many, many-to-one, many-to -many.
  • In object oriented programming, an object communicates to other object to use functionality and services provided by that object.
  • There are two forms of association
    • Aggregation
    • Composition
Q How aggregation and composition are different concepts?
  • In OOPS, Aggregation and composition are the types of association relations. A composition is a strong relationship. If the composite object is destroyed, then all its parts are destroyed. Example a car has a steering wheel if the car object is destroyed then there is no meaning of steering wheel.
  • In aggregation, the relationship is weaker then composition.
  • Example a library has students. If a library is destroyed, students exist. So library and student or related by aggregation. A library has books if Library is destroyed then books are also destroyed. Books of a library cannot exist without the library. So book and library Are related by composition.
  • In aggregation without existing container object if there is a chance of existing contained objects, then container and contained objects are weakly associated and this weak association is known as aggregation.
    • For example College consist of several Professors, without existing College, There may be a chance of existing professor objects, hence College and Professor objects are weakly associated and this is known as aggregation.
    • With aggregation,The college also performs its functions through Professor, But the professor is not always an internal part of the college. Professor may be swapped, Or even completely removed. Not only that, but the outside world can still have a reference to the professor, and tinker with it regardless of whether it’s in the college.
    • In aggregation container object holds just reference of the contained objects.
    • final class College {
      • private Professor professor
      • setProfessor();
      • getProfessor();
    • }
  • In composition without existing container object if there is no chance of existing contained objects, then container and contained objects are strongly associated and this strong association is known as composition.
    • In composition container object hold directly contained objects.
    • In the case of composition, the branches is completely encapsulated by the college. There is no way for the outside world to get other friends to the branches. The branches lives and dies with the college.
    • Final class College {
      • private final Branches branches;
      • College(BranchesNames names) {
        • branches = new Branches(names);
      • }
    • }
Q Why there are no pointers in Java?
  • In Java there are references instead of pointers. These references point to objects in memory but there is no direct access to these memory locations. JVM is free to move the objects within VM memory.
  • The absence of pointers helps Java in managing memory and garbage collection effectively. Also it provides developers with convenience of not getting worried about memory allocation and de-allocation.
  • Pointer variable must require static memory allocation but Java has dynamic memory allocation.
  • Pointer variables are supported by static languages in general but Java is a dynamic programming language.
  • Pointers are very suitable in platform dependent languages but Java is platform independent language.
  • Pointer variables refer address locations of data directly which is insecure.
Q What are the difference between pointer variables and reference variables?
  • Pointer variable refers to a block of memory by storing its address location. Pointer variables are recognised and initialised at compilation. Pointers are used in C/C++.
  • In reference variable JVM asks heap manager for memory.In heap depending on the class initialised. Heap manager creates a particular block of memory with an identity which has an integer value called as hash code of the Object. Heap manager returns the hash  code to the JVM. JVM Converts hash code To a hexadecimal value which is called as reference value Of an object. The reference value is assigned to the variable and variable is called as reference variable. Reference variables are recognised and initialised at runtime mainly. Reference variables are used in Java.
Q If there are no pointers In Java, then why do we get null pointer exception?
  • In Java the pointer equivalent is object reference. When we use a pointer it points to the object reference. So JVM uses pointers but programmers only see object references.
  • In case an object reference points to Null object, and we try to access a method or member variable on it, then we get NullPointerException.
Q What is the purpose of ‘super’ keyword in Java?
  • ‘super’ Keyword Is used in the methods or constructor of a child class. It refers to immediate parent class of an object.
  • By using ‘super’ We can call a method of parent class from the method of child class.
  • We can also call the constructor of a parent class from the constructor of a child class by using ‘super’ keyword.
Q Is it possible to use this() and super() Both in same constructor?
  • No, Java does not allow using both super() and this() in same constructor.
  • As per Java specification, super() and this() must be the first statement in a constructor.
Q What is the meaning of object cloning in Java?
  • Object.clone() Method is used for creating an exact copy of the object in Java. It acts like a copy constructor. It creates and returns a copy of the object, with the same class and with all the fields having same values as of the original object.
  • One disadvantage of cloning is that the return type is an object. It has to be explicitly cast to actual type.

No comments:

Post a Comment