JAVA
Inheritance
Prepared by
Miss. Arati A. Gadgil
2
Inheritance
Inheritance enables you to create a class that is similar to a previously
defined class, but one that still has some of its own properties, known as
code reusability.
When a class “B” inherits from or extends another class “A” then, A is
called the superclass of B, and B the subclass of A.
All the public and protected members of the superclass are available to
the subclass.
We can control the level of inheritance with the public, private, and
protected keywords.
Any class that is not explicitly declared as final can be extended.
When you inherit from an existing class, you can reuse methods and
fields of parent class, and you can add new methods and fields also.
Syntax
For one class to be a subclass of another, we use the extends keyword:
Class Marks
{
----------;
----------;
}
Class Result extends Marks
{
----------;
----------;
}
Super class
Sub class
Method overriding
Overriding refers a method in a subclass that has the same name, same
argument, and return type of a method in the superclass.
Implementation of this method in the subclass replaced the
implementation of the method in the superclass.
Example
Class Marks
{
public void display();
}
Class Result extends Marks
{
public void display();
}
5
Polymorphism
By using polymorphism, we can create new objects that perform the
same functions as the base object but which perform one or more
of these functions in a different way.
For example, you may have a shape object that draws a circle on the
screen. By using polymorphism, we can create a shape object that draws
a rectangle instead.
We can do this by creating a new version of the method that draws the
shape on the screen. Both the old circle-drawing and the new rectangle-
drawing method have the same name (such as DrawShape()) but drawing
in a different way.
6
Inheritance hierarchies
we may classify inheritance relationship into different categories
Specialization: the derived class is a special case of its base class, it
specializes its behavior and it is a subtype.
Generalization: the base class is obtained as result of finding common
behavior in different classes that become its children;
these derived classes override some of the methods in
the base class.
Specification: the base class defines some behavior that it is only
implemented in the derived class.
Extension: the derived class adds some behavior but does not
change the inherited behavior.
7
Combination: the derived class inherits some behavior from more
than one base class (multiple inheritance).
Construction: the derived class uses the behavior implemented in
the base class but it is not a subtype.
Limitation: the derived class restricts the use of some of the
behavior implemented in the base class.
Variance: the derived and base class are variants one of the other
and the relation class/subclass is arbitrary.
first two out of eight are common . The last three are not recommended.
On the other hand, while the origin and intent is different in all of the
different inheritance kinds, the result of applying them may be
indistinguishable.
8
In a generalization process the derived classes exist before and the base
class is obtained by realizing the commonalities in them.
In the specialization process the base class is ``broken down'' into
different derived classes. Inheritance hierarchies allow to manage a
system complexity.
We want all branches in an inheritance hierarchy to be disjoint and
balanced.
Disjoint means that any given object may not be an instance of two of
the subclasses found at the same level of the hierarchy.
By balanced we mean that two subclasses should represent comparable
sized sets of objects, having a very general class and a very particular
one at the same level of the hierarchy is not a good idea.
9
Inheritance can also be classified into static or dynamic.
In a static specialization an object belonging to a particular level of the
hierarchy is not intended to change in run-time from one subclass to
another.
In a dynamic specialization the belonging of the object to a particular
subclass depends on its state and therefore can change on run-time.
Dynamic inheritance, although theoretically correct, introduces many
practical problems and is often substituted, following
the delegation principle, by an association and a static inheritance.
10
Abstract classes
The class is called abstract if class contains one or more abstract method
Abstract classes must be inherited ,but not be instantiated.
It is not required that a subclass implement every abstract method in an
abstract superclass.
However, if all abstract methods are not implemented in the subclass, the
subclass must be declared abstract.
classes with all abstract methods are almost exactly like interfaces
11
Casting
Converting one type of value to another is called as Type Casting.
You can cast an instance of a child class to its parent class. Casting an
object of child class to a parent class is called upcasting.
Casting an object of a parent class to its child class is
called downcasting.
12
Types of Inheritance
1.Single
Class A
Class B
2.Multilevel
Class A
Class B
Class C
3.Multiple
Class A Class B
Class C
Multiple inheritance is not supported by java.
Consider A, B and C are three classes. The C class inherits A and B
classes. If A and B classes have same method and you call it from child
class object, there will be ambiguity to call method of A or B class.
13
The super keyword
Super keyword is used to differentiate the members of superclass from
the members of subclass, if they have same names.
It is used to call the superclass constructor from subclass constructor.
Final method and class
If any method is declared as a final then we can not override that method
If any class is declared as final then we can not extends that class.
Java Inner Class
Java inner class or nested class is a class i.e. declared inside
the class or interface.
Syntax of Inner class
class Java_Outer_class
{
//code
class Java_Inner_class
{
//code
}
}
Nested class
Non static
nested
Static nested
Member
class
Local
class
Anonymous
class
Types of Nested classes
•There are two types of nested classes non-static and static nested
classes.
•The non-static nested classes are also known as inner classes.
Non-static nested class(inner class)
a)Member inner class
A class created within class and outside method.
b)Annomynous inner class
A class created for implementing interface or
extending class. Its name is decided by the java compiler.
c)Local inner class
A class created within method
Static nested class
A static class created within class
class outer
{ int no;
outer()
{ no=10; }
class inner
{
void show()
{
System.out.println("no="+no);
}
}
public static void main(String args[])
{
outer obj1=new outer();
outer.inner obj2=obj1.new inner();
obj2.show();
}
}
Garbage collection
Since objects are dynamically allocated by using the new operator.
Java handles deallocation automatically. The technique that
accomplishes this is called garbage collection.
It works like this:
when no references to an object to exist, that object is assumed to
be no longer needed, and the memory occupied by the object can be
reclaimed.
Garbage collection only occurs sporadically (if at all) during the
execution of your program. It will not occur simply because one or
more objects exist that are no longer used.
 Furthermore, different java run-time implementations will take
varying approaches to garbage collection, but for the most part, you
should not have to think about it while writing your programs
The finalize () Method
 Sometimes an object will need to perform some action when it is 
destroyed. 
For example, if an object is holding some non-java resource such as a 
file handle or window character font, then you might want to make sure 
these resources are freed before an object is destroyed. 
To handle such situations, java provides a mechanism called 
finalization. By using finalization, you can define specific actions that 
will occur when an object is just about to be reclaimed by the garbage 
collector.
•To add a finalizer to a class, you simply define 
the finalize() method. 
•The java run time calls that method whenever it is about to recycle 
an object of that class. 
•Inside the finalize() method you will specify those actions that 
must be performed before an object is destroyed. 
•The garbage collector runs periodically, checking for objects that 
are no longer referenced by any running state or indirectly through 
other referenced objects.
• Right before an asset is freed, the java run time calls 
the finalize()method on the object.
The finalize() method has this general form:
 
            protected void finalize()
            {
             // finalization code here
            }
Thank You
22

Java inheritance

  • 1.
  • 2.
    2 Inheritance Inheritance enables youto create a class that is similar to a previously defined class, but one that still has some of its own properties, known as code reusability. When a class “B” inherits from or extends another class “A” then, A is called the superclass of B, and B the subclass of A. All the public and protected members of the superclass are available to the subclass. We can control the level of inheritance with the public, private, and protected keywords. Any class that is not explicitly declared as final can be extended. When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.
  • 3.
    Syntax For one classto be a subclass of another, we use the extends keyword: Class Marks { ----------; ----------; } Class Result extends Marks { ----------; ----------; } Super class Sub class
  • 4.
    Method overriding Overriding refersa method in a subclass that has the same name, same argument, and return type of a method in the superclass. Implementation of this method in the subclass replaced the implementation of the method in the superclass. Example Class Marks { public void display(); } Class Result extends Marks { public void display(); }
  • 5.
    5 Polymorphism By using polymorphism,we can create new objects that perform the same functions as the base object but which perform one or more of these functions in a different way. For example, you may have a shape object that draws a circle on the screen. By using polymorphism, we can create a shape object that draws a rectangle instead. We can do this by creating a new version of the method that draws the shape on the screen. Both the old circle-drawing and the new rectangle- drawing method have the same name (such as DrawShape()) but drawing in a different way.
  • 6.
    6 Inheritance hierarchies we mayclassify inheritance relationship into different categories Specialization: the derived class is a special case of its base class, it specializes its behavior and it is a subtype. Generalization: the base class is obtained as result of finding common behavior in different classes that become its children; these derived classes override some of the methods in the base class. Specification: the base class defines some behavior that it is only implemented in the derived class. Extension: the derived class adds some behavior but does not change the inherited behavior.
  • 7.
    7 Combination: the derivedclass inherits some behavior from more than one base class (multiple inheritance). Construction: the derived class uses the behavior implemented in the base class but it is not a subtype. Limitation: the derived class restricts the use of some of the behavior implemented in the base class. Variance: the derived and base class are variants one of the other and the relation class/subclass is arbitrary. first two out of eight are common . The last three are not recommended. On the other hand, while the origin and intent is different in all of the different inheritance kinds, the result of applying them may be indistinguishable.
  • 8.
    8 In a generalizationprocess the derived classes exist before and the base class is obtained by realizing the commonalities in them. In the specialization process the base class is ``broken down'' into different derived classes. Inheritance hierarchies allow to manage a system complexity. We want all branches in an inheritance hierarchy to be disjoint and balanced. Disjoint means that any given object may not be an instance of two of the subclasses found at the same level of the hierarchy. By balanced we mean that two subclasses should represent comparable sized sets of objects, having a very general class and a very particular one at the same level of the hierarchy is not a good idea.
  • 9.
    9 Inheritance can alsobe classified into static or dynamic. In a static specialization an object belonging to a particular level of the hierarchy is not intended to change in run-time from one subclass to another. In a dynamic specialization the belonging of the object to a particular subclass depends on its state and therefore can change on run-time. Dynamic inheritance, although theoretically correct, introduces many practical problems and is often substituted, following the delegation principle, by an association and a static inheritance.
  • 10.
    10 Abstract classes The classis called abstract if class contains one or more abstract method Abstract classes must be inherited ,but not be instantiated. It is not required that a subclass implement every abstract method in an abstract superclass. However, if all abstract methods are not implemented in the subclass, the subclass must be declared abstract. classes with all abstract methods are almost exactly like interfaces
  • 11.
    11 Casting Converting one typeof value to another is called as Type Casting. You can cast an instance of a child class to its parent class. Casting an object of child class to a parent class is called upcasting. Casting an object of a parent class to its child class is called downcasting.
  • 12.
    12 Types of Inheritance 1.Single ClassA Class B 2.Multilevel Class A Class B Class C 3.Multiple Class A Class B Class C Multiple inheritance is not supported by java. Consider A, B and C are three classes. The C class inherits A and B classes. If A and B classes have same method and you call it from child class object, there will be ambiguity to call method of A or B class.
  • 13.
    13 The super keyword Superkeyword is used to differentiate the members of superclass from the members of subclass, if they have same names. It is used to call the superclass constructor from subclass constructor. Final method and class If any method is declared as a final then we can not override that method If any class is declared as final then we can not extends that class.
  • 14.
    Java Inner Class Javainner class or nested class is a class i.e. declared inside the class or interface. Syntax of Inner class class Java_Outer_class { //code class Java_Inner_class { //code } }
  • 15.
    Nested class Non static nested Staticnested Member class Local class Anonymous class
  • 16.
    Types of Nestedclasses •There are two types of nested classes non-static and static nested classes. •The non-static nested classes are also known as inner classes. Non-static nested class(inner class) a)Member inner class A class created within class and outside method. b)Annomynous inner class A class created for implementing interface or extending class. Its name is decided by the java compiler. c)Local inner class A class created within method Static nested class A static class created within class
  • 17.
    class outer { intno; outer() { no=10; } class inner { void show() { System.out.println("no="+no); } } public static void main(String args[]) { outer obj1=new outer(); outer.inner obj2=obj1.new inner(); obj2.show(); } }
  • 18.
    Garbage collection Since objectsare dynamically allocated by using the new operator. Java handles deallocation automatically. The technique that accomplishes this is called garbage collection. It works like this: when no references to an object to exist, that object is assumed to be no longer needed, and the memory occupied by the object can be reclaimed. Garbage collection only occurs sporadically (if at all) during the execution of your program. It will not occur simply because one or more objects exist that are no longer used.  Furthermore, different java run-time implementations will take varying approaches to garbage collection, but for the most part, you should not have to think about it while writing your programs
  • 19.
    The finalize ()Method  Sometimes an object will need to perform some action when it is  destroyed.  For example, if an object is holding some non-java resource such as a  file handle or window character font, then you might want to make sure  these resources are freed before an object is destroyed.  To handle such situations, java provides a mechanism called  finalization. By using finalization, you can define specific actions that  will occur when an object is just about to be reclaimed by the garbage  collector.
  • 20.
  • 21.
  • 22.