Java Inheritance

Acquiring the properties of an existing Object into newly creating Object to overcome the redeclaration of properties in deferent classes.
These are 3 types:
1.Simple Inheritance:




    2.Multi Level Inheritance:
    

                       


    3. Multiple Inheritance:





Member access rules
Ø Visibility modifiers determine which class members are accessible and which do not
Ø Members (variables and methods) declared with public visibility are accessible, and those with private visibility are not
Ø Problem: How to make class/instance variables visible only to its subclasses?

Modifiers and Inheritance
Visibility Modifiers for class/interface:
public : can be accessed from outside the class definition.
protected : can be accessed only within the class definition in which it appears, within other classess in the same package, or within the definition of subclassess.
private : can be accessed only within the class definition in which it appears.
default-access (if omitted) features accessible from inside the current Java package
The protected Modifier
Ø The protected visibility modifier allows a member of a base class to be accessed in the child
Ø protected visibility provides more encapsulation than public does
Ø protected visibility is not as tightly encapsulated as private visibility

Example: Super-Class
class A {
int i;
void showi() {
System.out.println("i: " + i);
}
}
Extends:
It Is a keyword used to inherit a class from another class
Allows to extend from only one class
class One
{
int a=5;
}
class Two extends One
{
int b=10;
}

One baseobj;// base class object.
super class object baseobj can be used to refer its sub class objects.
For example, Two subobj=new Two;
Baseobj=subobj // now its pointing to sub class

Subclass, Subtype and Substitutability
Ø A subtype is a class that satisfies the principle of substitutability.
Ø A subclass is something constructed using inheritance, whether or not it satisfies the principle of substitutability.
Ø The two concepts are independent. Not all subclasses are subtypes, and (at least in some languages) you can construct subtypes that are not subclasses.
Ø Substitutability is fundamental to many of the powerful software development techniques in OOP.
Ø The idea is that, declared a variable in one type may hold the value of different type.
Ø Substitutability can occur through use of inheritance, whether using extends, or using implements keywords.
Ø When new classes are constructed using inheritance, the argument used to justify the validity of substitutability is as follows;
1.  Instances of the subclass must possess all data fields associated with its parent class.
2.  Instances of the subclass must implement, through inheritance at least, all functionality defined for parent class. (Defining new methods is not important for the argument.)
3.  Thus, an instance of a child class can mimic the behavior of the parent class and should be indistinguishable from an instance of parent class if substituted in a similar situation.
4. The term subtype is used to describe the relationship between types that explicitly recognizes the principle of substitution. A type B is considered to be a subtype of A if an instances of B can legally be assigned to a variable declared as of type A.
5. The term subclass refers to inheritance mechanism made by extends keyword.
6. Not all subclasses are subtypesSubtypes can also be formed using interface, linking types that have no inheritance relationship.
7. Subclass
Methods allows to reuse a sequence of statements
Inheritance allows to reuse classes by deriving a new class from an existing one
The existing class is called the parent class, or superclass, or base class 
The derived class is called the child class or subclass.
As the name implies, the child inherits characteristics of the parent(i.e the child class inherits the methods and data defined for the parent class
Subtype
¡ Inheritance relationships are often shown graphically in a class diagram, with the arrow pointing to the parent class



Substitutability (Deriving Subclasses)
In Java, we use the reserved word extends to establish an inheritance relationship

    class Animal
{
  // class contents
   int weight;
         public void int getWeight() {…}
}
class Bird extends Animal
{
   // class contents
         public void fly() {…};
}
Defining Methods in the Child Class:
Overriding by Replacement
A child class can override the definition of an inherited method in favor of its own
that is, a child can redefine a method that it inherits from its parent
the new method must have the same signature as the parent's method, but can have different code in the body
In java, all methods except of constructors override the methods of their ancestor class by replacement.  E.g.:
the Animal class has method eat()
the Bird class has method eat() and Bird extends Animal
variable b is of class Bird, i.e. Bird b = …
b.eat() simply invokes the eat() method of the Bird class
If a method is declared with the final modifier, it cannot be overridden




Previous Post Next Post