0% found this document useful (0 votes)
38 views

Java Programming6

The document discusses inheritance in Java including different types of inheritance (single, hierarchical, multilevel, multiple, hybrid), defining subclasses using the extends keyword, accessing members of superclasses using super, calling superclass constructors from subclass constructors using super(), and the order in which constructors are called (superclass constructor first, then subclass). It provides examples to illustrate these concepts.

Uploaded by

preetham a
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Java Programming6

The document discusses inheritance in Java including different types of inheritance (single, hierarchical, multilevel, multiple, hybrid), defining subclasses using the extends keyword, accessing members of superclasses using super, calling superclass constructors from subclass constructors using super(), and the order in which constructors are called (superclass constructor first, then subclass). It provides examples to illustrate these concepts.

Uploaded by

preetham a
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIT-II

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:

Derivation of a classes from another derived classes called 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

Systax: class subclass-name extends superclass-name

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.

Member access rules

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

/* In a class hierarchy, private members remain private to their class.


This program contains an error and will not
compile.
*/
// Create a superclass.
class A {
int i; // public by default
private int j; // private to A
void setij(int x, int y) {
i = x;
j = y;
}
}
// A's j is not accessible
here. class B extends A {
int total;
void sum() {
total = i + j; // ERROR, j is not accessible here

}
}
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);
}
}

Super class variables can refer sub-class object


o To a reference variable of a super class can be assigned a reference to any subclass
derived from that super class.
o When a reference to a subclass object is assigned to a super class reference
variable, we will have to access only to those parts of the object defined by the
super class
o It is bcz the super class has no knowledge about what a sub class adds to it.
Program

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);
}
}

Using super keyword

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

Using super to call super class constructor

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(); //

default BoxWeight mycube = new BoxWeight(3, 2);


BoxWeight myclone = new BoxWeight(mybox1);
double vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
System.out.println("Weight of mybox1 is " +
mybox1.weight); System.out.println();
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
System.out.println("Weight of mybox2 is " +
mybox2.weight); System.out.println();
vol = mybox3.volume();
System.out.println("Volume of mybox3 is " + vol);
System.out.println("Weight of mybox3 is " +
mybox3.weight); System.out.println();
vol = myclone.volume();
System.out.println("Volume of myclone is " + vol);
System.out.println("Weight of myclone is " +
myclone.weight); System.out.println();
vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
System.out.println("Weight of mycube is " +
mycube.weight); System.out.println();
}
}
Output:
Volume of mybox1 is 3000.0
Weight of mybox1 is 34.3
Volume of mybox2 is 24.0
Weight of mybox2 is 0.076
Volume of mybox3 is -1.0

Weight of mybox3 is -1.0


Volume of myclone is 3000.0
Weight of myclone is 34.3
Volume of mycube is 27.0
Weight of mycube is 2.0

Calling members of super class using super

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

When the constructor called:

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.");
}
}

// Create another subclass by extending


B. class C extends B {
C() {
System.out.println("Inside C's constructor.");
}
}
class CallingCons {
public static void main(String args[]) {
C c = new C();
}
}

Output:
Inside A’s constructor
Inside B’s constructor
Inside C’s constructor

You might also like