-
- OOP concept
- allows creating hierarchal classifications
- allows reuse of code
- allows any class to use/inherit the features – files + methods of other class
- Superclass – From which a class inherits its features
- no superclass can be its own superclass
- contains the general aspects of an object and the subclass can inherit from this to create specialised class
- Subclass
- the class which inherits all the features from the superclass and those can be accessed directly in the subclass
- can have its own fields and functions or methods and nested class
- super constructors can be invoked from subclass but they are not inherited
- does not inherit private member of superclass but they can be accessed by the public / protected getters/setters
- to use extends keyword to inherit a superclass
- when we create a new object of the subclass – all the superclass details will also be included in the memory space of the subclass => only subclass object is created which will have all properties of the referenced superclass
- can extend to only one superclass
- Subclass usage
- inherited fields can be used directly
- new fields in subclass used specific to subclass
- can use inherited methods directly
- can also “override” superclass method by using exact method signature
- can also “hide” superclass method by having a static method in subclass of same signature => opposite of overriding + if used static in method of a superclass then only superclass method will be invoked even if we create a subclass object
- can have subclass constructor as well
- using super
- “super” is used when a subclass needs to refer to its immediate superclass
- call superclass constructor
- subclass can call a constructor defined by its superclass as super(args) where arg list specifies the arguments for calling the superclass constructors
- thus even if the variables are private in superclass we can access them through the subclass
- super must be the first statement executed in a subclass constructor
- call superclass member
- super.member -> where this can be either a method or an instance variable
- when both subclass and superclass have same meter name => in subclass superclass members with same name will remain hidden
- Constructors are executed in the order of their derivations -> superclass>subclass
- OOP concept
- Member Access with Inheritance
- subclass cannot access any private members of a superclass as it is available only to the superclass
- Superclass variable can reference a subclass objects
- a reference variable of a superclass can be assigned a reference of any subclass
- superclass superObj = new subclass() => superclass >>> subclass
- and superclass reference object will have access only to the superclass members and not to the unique members of subclass because superclass does not know about subclass structure
- Types
- Single
- Multilevel -> a->b->c => c cannot access a directly
- Multiple -> extending multiple Interfaces
- why does java not support multiple inheritances?
- if the different superclass has same method signature a subclass inheriting both superclasses calling this method -> compiler will not be able to determine the method call or its priority
- why does java not support multiple inheritances?
- Object -> default superclass of all class and Object class does not have any superclass to it
- Wrt Constructors
- when we create a derived class object -> it will call super constructor directly – default constructor
- if we need to call the parameterized constructor we need to call the superclass constructor using super from subclass
- Interfaces
- Interfaces can extend more than one interfaces while a class can extend to only one class but implement multiple interfaces
- Final
- if a method is declared final in superclass – subclass cannot override the same
- a compile-time error if this method is tried to be overridden => early or static binding
- if a class is declared to be final, then that class cannot be inherited =>. Since we cannot modify the class there is no point in inheriting the class (?)
- any class is final => all its methods and fields are also final
- abstract and final cannot come together because any abstract class “needs” to rely on subclass for it to get a meaning
- if a method is declared final in superclass – subclass cannot override the same
- Method Overriding
- when both superclass and subclass have identical name methods[type and method signature] we say that subclass is overriding the superclass methods
- when an override method is called from within the subclass -> refers to the method in its class and not superclass=>makes sense since that is the method nearby and is needed
- the version of the method in superclass remains hidden
- now if we need to access superclass version of an override method we need to specifically call it using “super. methodname”
- if there is a difference in method signature then the method to be called among the overloaded methods will be determined by the values and other things
- Dynamic Method Dispatch
- method overriding is the basis of dynamic method dispatch which is => mechanism by which a call to an override method is resolved at run time and not at compile time => leads to the implementation of runtime polymorphism
- java uses the basic superclass reference holding subclass object for DMD -> when an override method is called through a superclass reference – java determines which method to be called based on the type of the object being referred to at the time of the call.
- it is the type of the object being referred to that determines which version of override method will be executed
- Why override methods ?
- allows runtime polymorphism
- polymorphism is essential because it allows a general class to specify methods that will be common to all its derivatives while allowing subclass to define the specific implementation for them
- one interface but multiple methods
- by combining inheritance with overridden methods = superclass can define general form of methods that will be used by all of its subclasses
- allows runtime polymorphism
- Apply MO
- we can define one consistent interface which can be used by several Dif and related types of objects
- Abstract Class
- sometimes we want to create a superclass in a generalized form which will be shared by all of its subclass which the only subclass can implement
- this occurs when the class does not have a meaningful definition at the context of the superclass
- certain methods can be given “abstract” modifier which will force the subclass to provide the implementation of the methods in them – by overriding them in their class
- abstract type name (parameter-list);
- no-body is present
- if any class has an abstract method then that class must be declared abstract
- no object can be created out an abstract class – cannot use new operator => there is no point in creating an object where there are no methods to be used
- cannot declare abstract constructors – because constructor must always do an instantiation in their body
- cannot use static with abstract in methods – static method can be invoked without any object – so if this static method have a call to an abstract method which has no body and can cause trouble and for any method, it needs a body
- any subclass if it does not implement all of the abstract methods of its superclass – needs to be declared as abstract
- concrete methods are also allowed in abstract classes
- though no object creations are possible with abstract classes we still can create object references and can be used to point to subclass reference – run time resolution of methods
- Final and inheritance
- can prevent overriding
- because final means constant and no changes area allowed
- when declared final – java compiler can copy byte code directly to compile code of calling method without any changes
- inlining is an option with final methods – resolving calls to method at compile time is called early binding and when method calls are resolved at run time it is called – late binding
- can prevent inheritance
- if a class is declared final – that class cannot be inherited
- all methods will by default be final
- => a class cannot be both abstract and final together => final class cannot be inherited where as abstract class must be inherited by its subclass
- can prevent overriding
- Object Class
- defined by java
- all other classes are sub classes of Object
- any reference variable of type object can refer to object of any other class inclusive of arrays
- Methods of object class
- clone() – creates a new object that is same as the object being cloned
- equals(Object obj) – determines if one object is equal to another
- finalize() – called before an unused object is recycled
- final – getClass() – obtains th class of the object at run time
- hashCode() – returns the hashcode of the invoking object
- final – notify() – resumes the execution go a thread waiting on invoking object
- final – notifyAll() – resumes the execution of all threads waiting on invoking object
- toString() – returns a string describing the object
- final – wait() – waits on another thread of execution