Java Module4 Bplck205c
Java Module4 Bplck205c
MODULE-4
Chapter 1: Inheritance
Syllabus:
Chapter 1:
1. Inheritance Basics
(i) Member Access and Inheritance
(ii) A Superclass Variable Can reference a Subclass Object
2. Using Super
(i) Using super to Call Superclass Constructor
(ii) A Second Use for super
3. Creating a Multilevel Hierarchy
4. When Constructors are Called
5. Method Overriding
6. Dynamic Method Dispatch
7. Using Abstract Classes
8. Using final with Inheritance
(i) Using final to Prevent Overriding
(ii) Using final to Prevent Inheritance
9. The Object Class
Chapter 1: Inheritance
Inheritance is the process by which one class acquires the properties of another
class. This is important because it supports the concept of hierarchical classification.
The class that is inherited is called superclass. The class that is inheriting the
properties is called subclass.
Therefore the subclass is the specialized version of the superclass. The subclass
inherits all the instance variable and methods, and adds its own code.
1. Inheritance Basics
The properties from superclass to the subclass are inherited using the "extends"
keyword.
General form of inheritance
The following program creates a superclass called A and a subclass called B. Notice
how the keyword extends is used to create a subclass of A.
class SimpleInheritance
{
public static void main(String args[])
{
/* subclass object creation, which acquires Contents of b:
all the properties of the superclass*/ i and j: 7 8
B b = new B(); k: 9
/* The subclass has access to all public Sum of i, j and k in b:
members of its superclass. */ i+j+k: 24
b.i = 7;
b.j = 8;
b.k = 9;
System.out.println("Contents of b: ");
b.showij();
b.showk();
System.out.println("Sum of i, j and k in b:");
b.sum();
}
}
Note:
This program will not compile because the reference to j inside the sum( )
method of B causes an access violation. Since j is declared as private, it is only
accessible by other members of its own class. Subclasses have no access to it.
class Box
{
double width,height,depth;
}
class Boxweight extends Box
{
double weight;
Boxweight(double x, double y, double z, double a)
{
width=x; height=y; depth=z; weight=a;
}
void volume()
{
System.out.println("The volume is :"+(width*height*depth));
}
}
class BoxDemo
{
//creating superclass object
public static void main(String args[])
{
Box b=new Box();
//creating the subclass object
Boxweight bw=new Boxweight(2,3,4,5);
bw.volume();
//assigning the subclass object to the superclass object
b=bw; // b has been created with its own data, in which weight is not a member
2. Using Super
Whenever a subclass needs to refer to its immediate superclass, it can do so by use
of the keyword super.
super has two general forms(uses).
The first calls the superclass’ constructor.
The second is used to access a member of the superclass that has been
hidden by a member of a subclass.
Here, arg-list, is the list of the arguments in the superclass constructor. Yhis must
be the first statement inside the subclass constructor.
For example:
(ii) Using super to access the super class members.(A second use for super)
The second form of super acts somewhat like this, except that it always refers to
the superclass of the subclass in which it is used. This usage has the following
general form:
super.member;
class A
{
void methodA()
{
System.out.println("This is the method of A");
}
}
//Multi Level Inheritance//class b extends the super class A
class B extends A
{
void methodB()
{
System.out.println("This is the method of B");
}
}//class C extends the super class B
class C extends B
{
void methodC()
{
System.out.println("This is the method of C");
}
}
class Hierarchy
{
public static void main(String args[])
{
C cobj=new C();
OUTPUT
cobj.methodA();
This is the method of A
cobj.methodB();
cobj.methodC(); This is the method of B
} This is the method of C
}
5. Method Overriding
In a class hierarchy, when a method in a subclass has the same name and type
signature as a method in its superclass, then the method in the subclass is said to
override the method in the superclass.
When an overridden method is called from within a subclass, it will always refer to
the version of that method defined by the subclass. The version of the method
defined by the superclass will be hidden.
Consider the following example:
// Method overriding.
class A
{
void disp()
{
System.out.println("This is the class A method");
}
}
// extending the superclass properties
OUTPUT
class B extends A
This is the class B method
{
void disp()
{
System.out.println("This is the class B method");
}
}
// main class
class OverRide
{
public static void main(String args[])
{
B b=new B();
b.disp(); // super class method is hidden, subclass method is invoked
}
}
GENERAL FORM
abstract type name (parameter-list);
class A
{
final void meth()
{
System.out.println("This is a final method.");
}
}
class B extends A
{
void meth() // ERROR! Can't override.
{
System.out.println("Illegal!");
}
}
final class A
{
// ...
}
// The following class is illegal.
class B extends A
{
// ERROR! Can't subclass A
// ...
}
Method Purpose
Object clone( ) Creates a new object that is the same as the object being
cloned.
boolean equals(Object object) Determines whether one object is equal to another.
void finalize( ) Called before an unused object is recycled.
Class getClass( ) Obtains the class of an object at run time.
int hashCode( ) Returns the hash code associated with the invoking object.
void notify( ) Resumes execution of a thread waiting on the invoking
object
void notifyAll( ) Resumes execution of all threads waiting on the invoking
object
String toString( ) Returns a string that describes the object.
void wait( ) Waits on another thread of execution.
void wait(long milliseconds)
void wait (long milliseconds,
I int nanoseconds)