Chap 06

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 18

Programming Java

Inheritance

Incheon Paik

1 Computer Industry Lab.


Java
Contents
 Subclasses
 Inheritance & Variables
 Method Overriding
 Inheritance & Methods
 Inheritance & Constructors
 Class Modifier
 Variables Modifier
 Constructor Modifier
 Method Modifier
 The Object and Class Classes

2 Computer Industry Lab.


Java
Subclasses
class X12 extends X1 {
Inheritance of a Class }

class clsName2 extends clsName1 { class X21 extends X2 {


// Body of class }
}
class X22 extends X2 {
}
Declaration of Variable of a Class class InheritanceHierarchy {
public static void main(String args[]) {
clsName varName; X x;
System.out.println("Instantiating X");
x = new X();
class X { System.out.println("Instantiating X1");
} Instantiating x = new X1();
System.out.println("Instantiating X11");
class X1 extends X { x = new X11();
} System.out.println("Instantiating X12");
x = new X12();
Inherit System.out.println("Instantiating X2");
class X2 extends X {
x = new X2();
} System.out.println("Instantiating X21");
x = new X21();
class X11 extends X1 { System.out.println("Instantiating X22");
} x = new X22();
}
}

3 Computer Industry Lab.


Java
Subclasses
class Wxyz {
Reference to a Subclass Variable public static void main(String args[]) {
Z z = new Z();
super.varName
z.f = 4.567f;
z.sb = new StringBuffer("abcde");
Inherited Variables z.s = "Teach Yourself Java";
class W { z.i = new Integer(41);
float f; System.out.println("z.f = " + z.f);
}
System.out.println("z.sb = " + z.sb);
System.out.println("z.s = " + z.s);
class X extends W {
System.out.println("z.i = " + z.i);
StringBuffer sb;
} }
}
class Y extends X {
String s;
} Result :
class Z extends Y { Inherit z.f = 4.567
Integer i; z.sb = abcde
}
z.s = Teach Yourself Java
z.i = 41

4 Computer Industry Lab.


Java
Subclasses
class P {
class E {
static int x;
int x;
}
}

class F extends E { class Q extends P {


String x; static String x;
} }

class Ef { class Pq {
public static void main(String args[]) { public static void main(String args[]) {
F f = new F(); P p = new P();
f.x = "This is a string";
Here, if f.x = 30; ?
p.x = 55;
System.out.println("f.x = " + f.x);
System.out.println("p.x = " + p.x);
E e = new E();
Q q = new Q();
e.x = 45;
q.x = "This is a string";
System.out.println("e.x = " + e.x);
} System.out.println("q.x = " + q.x);
} }
}
Result :
f.x = This is a string Result :
p.x = 55
e.x = 45
q.x = This is a string

5 Computer Industry Lab.


Java
Subclasses
class M100 {
int i = 100;
}

class M200 extends M100 {


int i = 200;
void display() {
System.out.println("i = " + i);
System.out.println("super.i = " + super.i);
}
}

class SuperKeyword {
public static void main(String args[]) {
M200 m200 = new M200();
m200.display();
}
}

Result :
i = 200
super.i = 100

6 Computer Industry Lab.


Java
Method Overriding
class C1 extends B1 {
void hello() {
Dynamic Method Dispatch Mechanism
System.out.println("Hello from C1");
}
}
Overloading & Overriding
class MethodOverriding1 {
public static void main(String args[]) {
Overriding : When the method with same si-
gnature is defined in Subclass C1 obj = new C1();
obj.hello();
Overloading : Only the same name, but
different in no. or type of parameter }
}

class A1 {
void hello() {
System.out.println("Hello from A1");
Result :
}
} Hello from C1
Overrided by hello of B1
class B1 extends A1 {
void hello() {
System.out.println("Hello from B1");
}
}

7 Computer Industry Lab.


Java
Method Overriding
class A2 {
void hello() { Result :
System.out.println("Hello from A2");
Hello from C2
}
}

class B2 extends A2 {
void hello() { Overrided by hello of B2
System.out.println("Hello from B2");
}
}

class C2 extends B2 {
void hello() {
System.out.println("Hello from C2");
}
}

class MethodOverriding2 {
public static void main(String args[]) {
A2 obj = new C2();
obj.hello();
} Object of C2
}

8 Computer Industry Lab.


Java
Inheritance and Methods
class SuperForMethods1 {
Reference of Methods in Superclass
public static void main(String args[]) {

super.mthName(args) System.out.println("Instantiating I1");


I1 obj = new I1();
class I1 { obj.hello("Good morning");
void hello(String s) {
System.out.println("I1: " + s); System.out.println("Instantiating J1");
} obj = new J1();
} obj.hello("Good afternoon");

class J1 extends I1 { System.out.println("Instantiating K1");


void hello(String s) { obj = new K1();
super.hello(s); obj.hello("Good evening");
System.out.println("J1: " + s); }
} }
}
Result :
Instantiating I1
class K1 extends J1 { I1: Good morning
void hello(String s) { Instantiating J1
super.hello(s); I1: Good afternoon
System.out.println("K1: " + s); J1: Good afternoon
} Instantiating K1
} I1: Good evening
J1: Good evening
K1: Good evening
9 Computer Industry Lab.
Java
Inheritance and Constructors
class U1 extends T1 {
int u1;
Invoking of Super-Constructor
U1() {
System.out.println("U1 Constructor");
super (args)
u1 = 3;
}
Invoking Constructor of Same Class }

this (args) class InheritanceAndConstructors1 {


public static void main(String args[]) {
U1 u1 = new U1();
class S1 {
System.out.println("u1.s1 = " + u1.s1);
int s1;
System.out.println("u1.t1 = " + u1.t1);
S1() {
System.out.println("u1.u1 = " + u1.u1);
System.out.println("S1 Constructor");
}
s1 = 1;
}
}
}
Result :
class T1 extends S1 { S1 Constructor
T1 Constructor
int t1; What does this
T1() { U1 Constructor mean?
System.out.println("T1 Constructor"); u1.s1 = 1
u1.t1 = 2
t1 = 2;
u1.u1 = 3
}
}

10 Computer Industry Lab.


Java
Inheritance and Constructors
class U2 extends T2 {
int u2;
Invocation Order of Constructor U2(int s2, int t2, int u2) {
super(s2, t2);
1) Constructor of Super Class this.u2 = u2;
2) Initializing Field }
3) Constructor Body }

class InheritanceAndConstructors2 {
public static void main(String args[]) {
U2 u2 = new U2(1, 2, 3);
class S2 {
System.out.println("u2.s2 = " + u2.s2);
int s2;
System.out.println("u2.t2 = " + u2.t2);
S2(int s2) {
System.out.println("u2.u2 = " + u2.u2);
this.s2 = s2;
}
}
}
}

Result :
class T2 extends S2 {
u2.s2 = 1
int t2;
u2.t2 = 2
T2(int s2, int t2) { u2.u2 = 3
super(s2);
this.t2 = t2;
}
}

11 Computer Industry Lab.


Java
Class Modifier
class Rectangle extends Shape {
void display() {
Qualifier of Class System.out.println("Rectangle");
}
abstract : Can not be instantiated }
final : Cannot extend class Triangle extends Shape {
void display() {
public : Can be referenced from all
classes. System.out.println("Triangle");
}
}
class AbstractClassDemo {
public static void main(String args[]) {
Abstract Class Shape s = new Circle();
s.display();
A class which has abstract method(s). s = new Rectangle();
It has not detailed implementation, an s.display();
d can be implemented in a subclass s = new Triangle();
s.display();
abstract class Shape { }
void display() { }
}
Result :
}
class Circle extends Shape { Circle
void display() { Rectangle
System.out.println("Circle");
Triangle
}
}
12 Computer Industry Lab.
Java
Variable Modifier
class L {
static final int x = 5;
Variable Modifier }

final : a constant class FinalVariable {


private : can be accessed only in the public static void main(String args[]) {
same class System.out.println(L.x);
protected : can be accessed from sub }
class and the same package }

public : Can be referenced from all cl


asses.
Result :
static : not instance variable
5
transient : variable which is not a par
t of consistent state of class
volatile : protect of optimization such
as constant propagation.

13 Computer Industry Lab.


Java
Constructor Modifier
class PrivateConstructor {

Qualifier of Constructor public static void main(String args[]) {

// public Constructor Invocation


private : can be accessed only in
same class Person p1 = new Person("John", 30);
System.out.println(p1.name);
protected : can be accessed from System.out.println(p1.age);
subclass and same package
public : Can be referenced from all // private constructor invoction
classes. // Person p2 = new Person();
}
}

class Person {
String name; What is the result of compilation when we removed
this comment?
int age;

public Person(String name, int age) {


this.name = name;
this.age = age; Current Result :
} John
30
private Person() {
}
}
14 Computer Industry Lab.
Java
Method Modifier
class DC10 extends JetPlane {
int numEngines() {
Method Modifier return 3;
}
}
abstract : method without implementation
final: cannot override class JetPlanes {
native : Machine code usage such as C or C public static void main(String args[]) {
++ System.out.println(new DC8().numEngines());
private : can be accessed only in the same System.out.println(new DC10().numEngines());
class }
}
protected : can be accessed from subclass
and the same package
public : Can be referenced from all classes.
static : not instance method
synchronized : when executed, lock among
threads

abstract class JetPlane {


abstract int numEngines(); Result :
}
class DC8 extends JetPlane { 4
int numEngines() { 3
return 4;
}
}
15 Computer Industry Lab.
Java
Method Modifier
class Singleton {
static Singleton singleton;

private Singleton() {
}

public static Singleton getInstance() { Result :


if (singleton == null) Equal
singleton = new Singleton();
return singleton;
}
}

class SingletonDemo {
public static void main(String args[]) {
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
if (s1 == s2)
System.out.println("Equal");
else
System.out.println("Not equal");
}
}

16 Computer Industry Lab.


Java
The Object & Class Classes
Object Class : Most Top Class Class
class in Java

equals() method getName() method

boolean equals(Object obj) String getName()

getClass() method getSuperClass() method

Class getClass() Class getSuperClass()

toString() method forName() method

String toString() Static Class forName (String cIsNam


e) throws ClassNotFoundException

17 Computer Industry Lab.


Java
The Object & Class Classes
class ClassDemo {

public static void main(String args[]) {

Integer obj = new Integer(8);


Class cls = obj.getClass();
System.out.println(cls); Result :
} Class java.lang.Integer
}

18 Computer Industry Lab.


Java

You might also like