Extend Class

Download Report

Transcript Extend Class

Extend Class
Extended Class
Method Override
Class Type Conversion
Class Design
Extend Class
Base Class
superclass
(base class)
Additional
Information
subclass
(derived class)
Extend Class

Form of Extend Class Definition
class SubClassName extends SuperClassName {
// Field Declaration
// Method Definition
}
Extend Class
class SuperClass {
int a;
void methodA {
// ...
}
}
class SubClass extends SuperClass {
int b;
void methodB {
// ...
}
}
Extend Class
Single Inheritance
 Object Class


Super class of all classes
Object
If we don’t reference super class explicitly,
the default super class is Object
superclass
subclass

Improve the re-usability via inheritance
Field of Extend Class

super

Refer the hidden field in the super class
class SuperClass {
int a = 1;
int b = 1;
}
class SubClass extends SuperClass {
int a = 2;
int b = super.a;
// . . .
}

[NameConflict.java]
Constructor of Extend Class

Execution Order



1) Call the constructor of super class
2) Execute the part of initializing the field
3) Execute the body of constructor


[SubConstructor.java]
super()

Explicit calling the constructor of super class

[SuperCallTest.java]
Method Overriding

Method Overloading


Differ regarding number and type of parameters
Method Overriding


Equal regarding number and type of parameter
Replaced with the method of subclass


The meaning of method on super class be changed in
subclass
[OverridingAndOverloading.java]
Method Overriding

Case of being not able to override the method


Static Method
Final Method

All methods in final class are final method implicitly
final class ClassA {
void methodA() /* ... */
void methodB() /* ... */
}

[Addendum.java]
Abstract Class

The Class which has abstract method

Abstract method :


Method which does not have real implementation but has only
method declaration
Declaration Method
abstract class AbstractClass {
public abstract void methodA();
void methodB() {
// ...
}
}
Abstract Class

No implementation, Gives the framework only



Abstract class cannot have object
To give the consistency when we use the method at other
out classes
Can be used after inherit by other class

The object can be created after all abstract methods are
implemented in subclass.

[AbstractClassTest.java]
When we implement the abstract method in subclas
We should use the same access modifier.
Class Type Conversion
Super Class
cast Operator
Automatic Conversion
Sub Class
Class Type Conversion
CLanguage
Java
void dummy(CLanguage obj) {
// …
}
// ...
Java j = new Java();
dummy(j);
// OK
Cplusplus
void dummy(Java obj) {
// …
}
// ...
Clanguage c = new CLanguage();
dummy(c);
// Error
dummy( (Java)c ); //Exception
Class Type Conversion

polymorphism

The meaning of method will be changed according to the
object applied.
CLanguage c = new Java();
c.print();


[Polymorphism.java]
This refers to an object of Java Class,
although type of c is CLanguage type
Class Design

Common fields and methods would be in
super class

When we define a class, it is important to
design it with hierarchical structure using
inheritance
Class Design
class AnsiC {
int declarations;
int operators;
int statements;
void functions() {
// ...
}
}
class Java extends AnsiC {
int classes;
int exceptions;
int threads;
}
class Cplusplus Extends AnsiC {
int classes;
int exceptions;
int operatorOverloadings;
}
class Oopl extends AnsiC {
int classes;
int exceptions;
}
class Java extends Oopl {
int threads;
}
class Cplusplus extends Oopl {
int operatorOverloadings;
}