Chapter 4
Chapter 4
2
Lookout the Following example
3
Cont.…
• The extended “extends” keyword in
Java programming is used to establish
inheritance between two classes.
class Superclass {
………..// Fields and methods of the superclass
}
class Subclass extends Superclass {
……// Additional fields and methods of the subclass
}
Here
• Extends: It establishes an inheritance relationship
between two classes. Indicates that the subclass is
inheriting from the superclass.
• Superclass: The base class that provides common
features to be inherited.
• Subclass: The derived class that inherits from the
5
superclass using extends.
Cont.
// A simple example of inheritance.
// Create a superclass.
class A {
int i, j;
void showij() {
System.out.println("i and j: " + i + " " + j);
}}
// 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));
6
}}
Cont.
hierarchy:
8
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); 9
A Superclass Variable Can Reference a Subclass Object
12
Using super
• The second is used to access a member of the superclass that has been
super(arg-list);
• Here, arg-list specifies any arguments needed by the constructor in the
superclass.
• super( ) must always be the first statement executed inside a subclass’
13
Cont.
// BoxWeight now uses super to initialise 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;
}}
14
Cont.
• Here, BoxWeight( ) calls super( ) with the arguments w,
h, and d.
16
Cont.
• Multilevel inheritance: refers to a child and parent class
relationship where a class extends the child class.
17
Cont.
• Hierarchical inheritance: refers to a child and parent class
relationship where more than one classes extends the same
class.
• For example, classes B, C & D extends the same class A.
18
Cont.
• Multiple Inheritance: refers to the concept of one class
extending more than one classes, which means a child
class has two parent classes.
19
When Constructors Are Called
20
class A {
A() { // Constructor
System.out.println("Inside A's constructor.");
}}
class B extends A {
B() {
System.out.println("Inside B's constructor.");
}}
class C extends B {
C() {
System.out.println("Inside C's constructor.");
}}
class CallingCons {
public static void main(String args[]) {
C c = new C();
21
}}
Method Overriding
22
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
void show() {
System.out.println("i and j: " + i + " " + j);
}}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
void show() {
System.out.println("k: " + k);
}}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B 23
Cont.
• The output produced by this program is:
k: 3
• When show( ) is invoked on an object of type B, the version of
show( ) defined within B is used.
• That is, the version of show( ) inside B overrides the version
declared in A.
• If you wish to access the superclass version of an overridden
method, you can do so by using super.
• For example, in this version of B, the superclass version of show(
) is invoked within the subclass’ version.
24
• This allows all instance variables to be displayed.
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
void show() {
super.show(); // this calls A's show()
System.out.println("k: " + k);
}}
If you substitute this version of A into the previous
program, you will see the following output:
i and j: 1 2
k: 3
25
Overridden Methods
• Any class that contains one or more abstract methods must also be
declared abstract.
abstract class A {
void callmetoo() {
}}
class B extends A {
void callme() {
}}
class AbstractDemo {
B b = new B();
b.callme();
28
b.callmetoo();
Polymorphism
• Polymorphism is one of the OOPs feature that allows us to perform a
• For example, lets say we have a class Animal that has a method sound().
...
...
@Override
System.out.println("Neigh");
30
}}
Cont.
and
public class Cat extends Animal{
...
@Override
public void sound(){
System.out.println("Meow");
}
}
31
Encapsulation
• Encapsulation is the wrapping up of data under a single unit.
• It is the mechanism that binds together code and the data it manipulates.
• It is a protective shield that prevents the data from being accessed by the
code outside this shield.
• To allow the outside access to the instance variables, public methods
called getters and setters are defined, which are used to retrieve and
modify the values of the instance variables, respectively.
• By using getters & setters, the class can enforce its own data
validation rules and ensure that its internal state remains consistent.
• Encapsulation links data with the code that manipulates it.
• Encapsulation provides an important attribute: access control.
• Through encapsulation, you can control what parts of a program can
32
access the members of a class.
Cont.
• A subclass (child class) inherits from the superclass and potentially adds its own
unique features.
• To call a superclass method from the overriding subclass method, prefix the method
name with the reserved word “super "and the member access operator.
• Java uses the extends that specifies a parent-child relationship b/n two classes.
• The extends keyword is specified after the class name and before another class name.
• The class name before extends identifies the child and the class name after extends
• It is impossible to specify multiple class name after extends, b/c java doesn’t support
35
THE END
THANK YOU !!!
36