Java Programming6
Java Programming6
Inheritance – Inheritance types- super keyword- preventing inheritance: final classes and
methods
Polymorphism – method overloading and method overriding, abstract classes and methods.
Interface – Interfaces VS Abstract classes- defining an interface- implement interfaces-
accessing implementations through interface references- extending interface -inner classes.
Packages – Defining- creating and accessing a package- importing packages.
Types of Inheretence:
Single Inheretence
Hierarichal Inherintence
Multiple Inherintence
Multilevel Inherintence
Hybrid Inherintence
Single Inherintence:
Derivation a subclass from only one super class is called Single Inherintence.
Hierarchical Inherintence:
Derivation of several classes from a single super class is called Hierarchical Inherintence:
Multilevel Inheritance:
Multiple Inheritance:
Derivation of one class from two or more super classes is called Multiple Inheritance
But java does not support Multiple Inheritance directly. It can be implemented by using interface
concept.
Hybrid Inheritance:
Derivation of a class involving more than one from on Inheritance is called Hydrid Inheritance
Defining a Subclass:
A subclass is defined as
Variable declaration;
Method declaration;
}
The keyword extends signifies that the properties of the super class name are extended to the
subclass name. The subclass will now contain its own variables and methods as well as those of
the super class. But it is not vice-versa.
o Even though a subclass includes all of the members of its super class, it cannot access
those members who are declared as Private in super class.
o We can assign a reference of super class to the object of sub class. In that situation we
can access only super class members but not sub class members. This concept is called
as
―Super clas s Reference, Sub clas s Ob
}
}
class Access {
public static void main(String args[]) {
B subOb = new B();
subOb.setij(10, 12);
subOb.sum();
System.out.println("Total is " + subOb.total);
}
}
class RefDemo
{
public static void main(String args[])
{
BoxWeight weightbox = new BoxWeight(3, 5, 7,
8.37); Box plainbox = new Box();
double vol;
vol = weightbox.volume();
System.out.println("Volume of weightbox is " +
vol); System.out.println("Weight of weightbox is " +
weightbox.weight);
System.out.println();
// assign BoxWeight reference to Box
reference plainbox = weightbox;
vol = plainbox.volume(); // OK, volume() defined in Box
System.out.println("Volume of plainbox is " + vol);
/* The following statement is invalid because plainbox
does not define a weight member. */
// System.out.println("Weight of plainbox is " + plainbox.weight);
}
}
o When ever a sub class needs to refer to its immediate super class, it can do so by use
of the key word super.
o Super has two general forms:
o Calling super class constructor
o Used to access a member of the super class that has been hidden by a member
of a sub class
o A sub class can call a constructor defined by its super class by use of the following form
of super:
o super (parameter-list);
o Parameter list specifies parameters needed by the constructor in the super class.
Note: Super ( ) must always by the first statement executed inside a sub-class
constuctor.
// A complete implementation of BoxWeight.
class Box {
private double width;
private double height;
private double depth;
// construct clone of an object
Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to
indicate height = -1; // an
uninitialized depth = -1; // box
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
// BoxWeight now fully implements all constructors.
class BoxWeight extends Box {
double weight; // weight of box
// construct clone of an object
BoxWeight(BoxWeight ob) { // pass object to
constructor super(ob);
weight = ob.weight;
}
// constructor when all parameters are specified
BoxWeight(double w, double h, double d, double m) {
super(w, h, d); // call superclass constructor
weight = m;
}
// default constructor
BoxWeight()
{ super();
weight = -1;
}
// constructor used when cube is created
BoxWeight(double len, double m) {
super(len);
weight = m;
}
}
class DemoSuper {
public static void main(String args[]) {
BoxWeight mybox1 = new BoxWeight(10, 20, 15,
34.3); BoxWeight mybox2 = new BoxWeight(2, 3, 4,
0.076); BoxWeight mybox3 = new BoxWeight(); //
o The second form of super acts somewhat like this keyword, except that it always refers
to the super class of the sub class in which it is used.
o The syntax is:
o Super.member ;
o Member can either be method or an instance variable
Program
// Using super to overcome name
hiding. class A {
int i;
}
// Create a subclass by extending class A.
class B extends A {
int i; // this i hides the i in A
B(int a, int b) {
super.i = a; // i in A
i = b; // i in B
}
void show() {
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);
}
}
class UseSuper {
public static void main(String args[]) {
B subOb = new B(1, 2);
subOb.show();
}
}
Output:
i in superclass: 1
i in subclass: 2
Always the super class constructor will be executed first and sub class constructor will be
executed last.
// Demonstrate when constructors are called.
// Create a super class.
class A {
A() {
System.out.println("Inside A's constructor.");
}
}
// Create a subclass by extending class A.
class B extends A {
B() {
System.out.println("Inside B's constructor.");
}
}
Output:
Inside A’s constructor
Inside B’s constructor
Inside C’s constructor