Inheritance
- Introduction
- Diamond problem
Polymorphism
- Static polymorphism
- Dynamic polymorphism
- instanceOf keyword
- abstract keyword
- final keyword
- Special note on protected
Introduction
In OOP, we often organize classes in hierarchy to avoid duplication and reduce redundancy. The classes in the lower hierarchy inherit all the variables (attributes) and methods (dynamic behaviors) from the higher hierarchies.
Inheritance : generalization —-> specialization.
IS A Relationship.
sub class IS A super class and something added(additonal state + additional behaviour) and something modified(behaviour —method overriding)
Types of inheritance
- single inheritance —
class A{…} class B extends A{…} - multi level inhertance
class A{…} class B extends A{…} class C extends B{…} - multiple inhertiance — NOT supported
class A extends B,C{…} — compiler err
Why Multiple inheritence not supported – For simplicity.
(Diamond problem) -We have two classes B and C inheriting from A. Assume that B and C are overriding an inherited method and they provide their own implementation. Now D inherits from both B and C doing multiple inheritance. D should inherit that overridden method. BUT which overridden method will be used? Will it be from B or C? Here we have an ambiguity.
Polymorphism – one functionality –multiple (changing) forms
Two types
- Static
- Dynamic
- static — compile time –early binding —resolved by javac.
Achieved via method overloading
Rules
- can be in same class or in sub classes.
- same method name+
- signature — different (no/type/both)
- ret type — ignored by compiler.
- when javac doesn’t find exact match –tries to resolve it by the closest arg type(just wider than the specified arg)
void test(int i,int j){...}
void test(int i) {..}
void test(double i){..}
void test(int i,double j,boolean flag){..}
int test(int a,int b){...} ---> compiler err.
- Dynamic polymorphism — late binding — dynamic method dispatch —resolved by JRE.
Dynamic method dispatch — which form of method to send for execution —This decision can’t be taken by javac — BUT taken by JRE
Achieved via — method overriding
All java methods can be overridden : if they are not marked as private,static,final
Rules : to be followed by overriding method in a sub-class
- same method name, same signature, ret type must be same or its sub-type(co-variance)
eg of co-variance
class A {
A getInstance()
{
return new A();
}
}
class B extends A
{
B getInstance()
{
return new B();
}
}
- scope—must be same or wider.
- Can not add in its throws clause any new or broader checked exceptions. BUT can add any new unchecked excs. Can add any subset or sub-class of checked excs.
class A
{
void show() throws IOExc
{...}
}
class B extends A
{
void show() throws Exc
{...}
}
Regarding co-variance
Overrding form of method —
has same method name, same signature, ret type must be same or its sub-type(co-variance)
//eg of co-variance
class A {
A getInstance()
{
return new A();
}
}
class B extends A
{
B getInstance()
{
return new B();
}
}
Note- For private / static / final methods — compile time binding takes place.
Detailed working of dynamic polymorphism
Super -class ref. can directly refer to sub-class object(direct=w/o type casting) as its the example of up-casting(similar to widening auto. conversion) .
When such a super class ref is used to invoke the overriding method: which form of the method to send for execution : this decision is taken by JRE & not by compiler. In such case — overriding form of the method(sub-class version) will be dispatched for exec.
eg : A ref=new B(); ref.show() --->
// this will invoke the sub-class: overriding form of the show () method

//Example
public class A {
int i = 10;
public static void main(String[] args) {
A ref=new A();
System.out.println(ref.i);//10
B ref2=new B();
System.out.println(ref2.i);//20
A ref3=new B();
System.out.println(ref3.i); //10 //dm not overriden
}
}
class B extends A {
int i = 20;
}
class C extends A {
void fun(){
int i = 30;
System.out.println(i);
}
}
Example-2
public class A {
static void test() {
System.out.println("in A's test");
}
public static void main(String[] args) {
A b1 = new B();
b1.test();//inA's Test
B.test();
}
}
class B extends A {
//@Override
static void test() {
System.out.println("in B's test");
}
}
/*
* NOTE since static methods cannot be overridden aand b1 ref is of A type by
* javac(since javac uses ref type)
*/
Inheritance & polymorphism in java inbuild classes
Object class method
public String toString() —Rets string representation of object.
Returns — Fully qualified class Name @ hash code
hash code –internal memory representation.(hash code is mainly used in hashing based data structures — will be done in Collection framework)
Why override toString?
To replace hash code version by actual details of any object.
Need of overriding equals method ?
To replace reference equality by content equality , based upon prim key criteria.
instanceOf keyword –
used for testing run time type information
eg ---
Emp e =new Mgr(...);
if (e instanceof Mgr) ---rets true iff e --->Mgr obj
if (e instanceof Worker) ---rets true iff e --->Worker obj
if (e instanceof Emp)
abstract : keyword in Java
abstract methods —methods only with declaration & no definition
eg : public abstract double calNetsalry();
Any time a class has one or multilple abstract methods —- class must be declared as abstract class.
eg. public abstract class Emp {….}
Abstract classes can’t be instantiated BUT can create the ref. of abstract class type to refer to concrete sub-class instances.
Emp e1=new Emp(…);//illegal
Emp e1=new Mgr(….);//legal
Abstract classes CAN HAVE concrete(non-abstract) methods.
Abstract classes MUST provide constructor/s to init its own private data members.(through sub-class or implementation class)
Can a class be decalred as abstract & final ? NO
Can an abstract class be crerated with 100% concrete functionality?
Yes
eg — Event adapter classes
final — keyword in java
Usages
- final data member(primitive types) – constant
- eg — public final int data=123;
- final methods —can’t be overridden.
usage eg public final void show{…..}
eg — Object class — wait , notify ,notifyAll - final class — can’t be sub-classed(or extended) — i.e stopping inheritance hierarchy.
eg — String ,StringBuffer,StringBuilder - final reference — references can’t be re-assigned.
eg — final Emp e=new Mgr(…….);
e=new Worker(…..);//compiler err
Special note on protected
Protected members acts as default scope within the same package.
BUT outside pkg — a sub-class can access it through inheritance(i.e just inherits it directly) & CANT be accessed by creating super class instance.
One thought on “Java Tutorial”