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

Lecture 07 - Inheritance 1

This document discusses inheritance in object-oriented programming. It defines inheritance as creating a general superclass that defines common traits, which can then be inherited by more specific subclasses. The subclasses extend the superclass and can add their own unique attributes and behaviors. The document provides examples of declaring subclasses that extend a superclass, constructing objects of the subclass type, and accessing members of both the superclass and subclass. It also discusses using the super keyword to invoke the superclass constructor from within the subclass constructor.

Uploaded by

Muhammad Tayyab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Lecture 07 - Inheritance 1

This document discusses inheritance in object-oriented programming. It defines inheritance as creating a general superclass that defines common traits, which can then be inherited by more specific subclasses. The subclasses extend the superclass and can add their own unique attributes and behaviors. The document provides examples of declaring subclasses that extend a superclass, constructing objects of the subclass type, and accessing members of both the superclass and subclass. It also discusses using the super keyword to invoke the superclass constructor from within the subclass constructor.

Uploaded by

Muhammad Tayyab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

CSE205: Object Oriented

Programming
Lecture # 07: Inheritance

Muhammad Imran
(Based on Java, The Complete Reference)
http://www.secscourses.tk

1
Outline
• Inheritance
• A Superclass Variable can Reference a Subclass
Object
• Using super

2
Inheritance
• We can create a general class that defines traits common to a set of
related items
• This class can then be inherited by others, more specific classes,
each adding those things that are unique to it
• A class that is inherited is called a superclass.
• The class that does the inheriting is called a subclass.
• Therefore, a subclass is a specialized version of a superclass.

3
Inheritance
• To inherit a class, we simply incorporate the definition of one class
into another by using the extends keyword.
• The general form of a subclass declaration is:
class subclass-name extends superclass-name
{ /*body of class*/ }
• We can specify only one superclass for any subclass.
• This is because Java does not support multiple inheritance.

4
Inheritance
// A simple example of inheritance.
// Create a superclass.
class A {
int i, j;
void showij() {
System.out.println("i and j: " + i + " " + j);
}
}

Program continues on next slide …


5
Inheritance
// Create a subclass by extending class A.
class B extends A {
int k;
void showk() {
System.out.println("k: " + k);
}
void sum() {
System.out.println("i+j+k: " + (i+j+k));
}
}

Program continues on next slide …


6
class SimpleInheritance {
public static void main(String args[]) {
A superOb = new A();
B subOb = new B();
superOb.i = 10;
superOb.j = 20;
System.out.println("Contents of superOb: ");
superOb.showij();
System.out.println();
subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
System.out.println("Contents of subOb: ");
subOb.showij(); Contents of superOb:
subOb.showk(); i and j: 10 20
System.out.println(); Contents of subOb:
System.out.println("Sum of i, j and k in subOb:"); i and j: 7 8
subOb.sum(); k: 9

} Sum of i, j and k in subOb:


} i+j+k: 24 7
Member Access and Inheritance

• Although a subclass includes all of the members of its

superclass, it cannot access those members of the

superclass that have been declared as private.

8
A More Practical Example
// This program uses inheritance to extend Box.
class Box {
double width;
double height;
double depth;
// construct clone of an object
Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Program continues on next slide …
9
A More Practical Example
// 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
}
Program continues on next slide …
10
A More Practical Example
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}

// compute and return volume


double volume() {
return width * height * depth;
}
}

Program continues on next slide …


11
A More Practical Example
// Here, Box is extened to include weight.
class BoxWeight extends Box {
double weight; // weight of box
// constructor for BoxWeight
BoxWeight(double w, double h, double d, double m) {
width = w;
height = h;
depth = d;
weight = m;
}
}
Program continues on next slide …
12
class DemoBoxWeight {
public static void main(String args[]) {
BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);
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);
} Volume of mybox1 is 3000.0
Weight of mybox1 is 34.3
}
Volume of mybox2 is 24.0
Weight of mybox2 is 0.076
13
Examples for Inheritance (UML Class Diagram)

Can we Implement this design???


14
Multiple Levels of Inheritance (UML Class Diagram)

Can we Implement this design???


15
Multiple Levels of Inheritance (UML Class Diagram)

Can we Implement this design???


16
A Superclass Variable can Reference a
Subclass Object
• A reference variable of a superclass can be assigned a
reference to any subclass derived from that superclass.
SuperClass referenceVariable=new SubClass();

Or

SubClass subClassReference=new SubClass();

SuperClass referenceVariable=subClassReference;

17
A Superclass Variable can Reference a
Subclass Object
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();
plainbox = weightbox;
vol = plainbox.volume(); // OK, volume() defined in Box
System.out.println("Volume of plainbox is " + vol);
// System.out.println("Weight of plainbox is " + plainbox.weight);
}
} 18
A Superclass Variable can Reference a
Subclass Object
• It is important to understand that it is the type of the reference
variable – not the type of the object that it refers to – that
determines what members can be accessed.

• When a reference to a subclass object is assigned to a superclass


reference variable, we will have access only to those parts of the
object defined by the superclass.

19
Using super
BoxWeight(double w, double h, double d, double m) {
width = w; height = h;
depth = d; weight = m;
}
• The subclass constructor explicitly initializes instance
variables of superclass.
 This duplicates the code found in superclass.
 It implies that a subclass must be granted access to these members.
• What if the superclass data is kept private?
• super gives the solution. Whenever a subclass needs to refer
to its immediate superclass, it can do so by use of the
keyword super.

20
Using super
• super has two general forms:
 The 1st calls the superclass constructor.

 The 2nd is used to access a member of the superclass that has


been hidden by a member of a subclass

21
Using super to Invoke Superclass Constructor

• A subclass can call a constructor method defined by its


superclass by use of the following form of super:
super(parameter-list);

• super() must always be the first statement executed


inside a subclass’ constructor.

22
Using super to Invoke Superclass Constructor
// BoxWeight now uses super to initialize its Box attributes.
class BoxWeight extends Box {
double weight; // weight of box

// initialize width, height, and depth using super()


BoxWeight(double w, double h, double d, double m) {
super(w, h, d); // call superclass constructor
weight = m;
}
}

23
Using super to Invoke Superclass Constructor

• Since constructors can be overloaded, super() can be

called using any form defined by the superclass.

• The constructor executed will be the one that matches the

arguments.

24
Using super to Invoke Superclass Constructor

// 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;
}
Program continues on next slide …
25
Using super to Invoke Superclass
Constructor
// 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
}
Program continues on next slide …
26
Using super to Invoke Superclass
Constructor
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}

// compute and return volume


double volume() {
return width * height * depth;
}
}

Program continues on next slide …


27
Using super to Invoke Superclass
Constructor
// 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
When invoking
super(ob); Box(Box ob),
weight = ob.weight; super() is called
with an object of
} type BoxWeight –
// constructor when all parameters are specified not of type Box.
This is because a
BoxWeight(double w, double h, double d, double m) { superclass
super(w, h, d); // call superclass constructor variable can be
used to reference
weight = m; any object derived
} from that class.
Program continues on next slide …
28
Using super to Invoke Superclass
Constructor
// default constructor
BoxWeight() {
super();
weight = -1;
}

// constructor used when cube is created


BoxWeight(double len, double m) {
super(len);
weight = m;
}
}
Program continues on next slide …
29
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 = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
System.out.println("Weight of mybox1 is " + mybox1.weight);
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
System.out.println("Weight of mybox2 is " + mybox2.weight);
vol = mybox3.volume();
System.out.println("Volume of mybox3 is " + vol);
System.out.println("Weight of mybox3 is " + mybox3.weight);
vol = myclone.volume();
System.out.println("Volume of myclone is " + vol);
System.out.println("Weight of myclone is " + myclone.weight);
vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
System.out.println("Weight of mycube is " + mycube.weight);
}
} 30
Using super to Access Hidden Members of
Superclass
• 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.
• It has the following general form
super.member
• This form of super is most applicable to situations in which
member names of a subclass hide members by the same
name in the superclass.

31
Using super to Access Hidden
Members of Superclass
// 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
}
32
Using super to Access Hidden Members
of Superclass
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();
} i in superclass: 1
i in subclass: 2
}
33
Recommended Readings
• Page # 161 to 174, Chapter # 8: Inheritance from Herbert
Schildt, Java: The Complete Reference, J2SETM 9th Edition

34

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy