0% found this document useful (0 votes)
53 views15 pages

ExtendClass 1

This document discusses extending classes in Java. It explains that a subclass inherits and can override or extend the functionality of its superclass. A subclass extends a superclass by specifying the superclass name after the extends keyword in its class declaration. The subclass inherits fields and methods from the superclass but can also define new fields and methods of its own or override existing superclass methods. The document also covers method overriding, abstract classes, class type conversion, and principles of good class design using inheritance.

Uploaded by

muttuswami
Copyright
© Attribution Non-Commercial (BY-NC)
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)
53 views15 pages

ExtendClass 1

This document discusses extending classes in Java. It explains that a subclass inherits and can override or extend the functionality of its superclass. A subclass extends a superclass by specifying the superclass name after the extends keyword in its class declaration. The subclass inherits fields and methods from the superclass but can also define new fields and methods of its own or override existing superclass methods. The document also covers method overriding, abstract classes, class type conversion, and principles of good class design using inheritance.

Uploaded by

muttuswami
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 15

Extend Class

Extended Class
Method Override
Class Type Conversion
Class Design
Extend Class
superclass
superclass
Base (base
(baseclass)
BaseClass
Class class)

Additional
Additional
Information
Information
subclass
subclass
(derived
(derivedclass)
class)
Extend
ExtendClass
Class

q Form of Extend Class Definition


class
classSubClassName
SubClassNameextends
extendsSuperClassName
SuperClassName{{
////Field
FieldDeclaration
Declaration
////Method
MethodDefinition
Definition
}}
Extend Class

class
classSuperClass
SuperClass{{
int
inta;a;
void
voidmethodA
methodA{{
////......
}}
}}
class
classSubClass
SubClassextends
extendsSuperClass
SuperClass{{
int
intb;b;
void
voidmethodB
methodB{{
////......
}}
}}
Extend Class

q Single Inheritance
q Object Class
2 Super class of all classes
Object IfIfwe
wedon’t
don’treference
referencesuper
superclass
classexplicitly,
explicitly,
Object the default super class is Object
the default super class is Object

superclass
superclass

subclass
subclass

q Improve the re-usability via inheritance


Field of Extend Class

q super
2 Refer the hidden field in the super class
class
classSuperClass
SuperClass{{
int
intaa==1;1;
int
intbb==1;1;
}}
class
classSubClass
SubClassextends
extendsSuperClass
SuperClass{{
int
intaa==2;2;
int
intbb==super.a;
super.a
super.a;
////. .. .. .
}}

& [NameConflict.java]
Constructor of Extend Class
q Execution Order
2 1) Call the constructor of super class
2 2) Execute the part of initializing the field
2 3) Execute the body of constructor
& [SubConstructor.java]

q super()
2 Explicit calling the constructor of super class
& [SuperCallTest.java]
Method Overriding

q Method Overloading
2 Differ regarding number and type of parameters

q Method Overriding
2 Equal regarding number and type of parameter
2 Replaced with the method of subclass
4 The meaning of method on super class be changed in
subclass

& [OverridingAndOverloading.java]
Method Overriding

q Case of being not able to override the method


2 Static Method
2 Final Method
4 All methods in final class are final method implicitly

final
finalclass
classClassA
ClassA{{
void
voidmethodA()
methodA() /*/*......*/*/
void
voidmethodB()
methodB() /*/*......*/*/
}}

& [Addendum.java]
Abstract Class

q The Class which has abstract method


2 Abstract method :
4 Method which does not have real implementation but has only
method declaration

q Declaration Method

abstract
abstractclass
classAbstractClass
AbstractClass{{
public
publicabstract
abstractvoid
voidmethodA();
methodA();
void
voidmethodB()
methodB(){{
////......
}}
}}
Abstract Class

q No implementation, Gives the framework only


2 Abstract class cannot have object
2 To give the consistency when we use the method at other
out classes

q Can be used after inherit by other class


2 The object can be created after all abstract methods are
implemented in subclass.
When
Whenweweimplement
implementthe
theabstract
abstractmethod
methodininsubclass,
subclass,
& [AbstractClassTest.java] We
Weshould
shoulduse
usethe
thesame
sameaccess
accessmodifier.
modifier.
Class Type Conversion

Super
SuperClass
Class

cast Operator Automatic Conversion

Sub
SubClass
Class
Class Type Conversion

CLanguage
CLanguage

Java
Java Cplusplus
Cplusplus

void
voiddummy(CLanguage
dummy(CLanguageobj)
obj){{ void
voiddummy(Java
dummy(Java obj)
obj){{
////…… ////……
}} }}
////...... ////......
Java
Java j j==newnewJava();
Java(); Clanguage
Clanguage cc==new newCLanguage();
CLanguage();
dummy(j);
dummy(j); ////OK
OK dummy(c);
dummy(c); ////Error
Error

dummy(
dummy((Java)c
(Java)c);); //Exception
//Exception
Class Type Conversion

q polymorphism
2 The meaning of method will be changed according to the
object applied.
This
Thisrefers
referstotoan
anobject
objectofofJava
JavaClass,
Class,
CLanguage
CLanguage cc==new
newJava();
Java(); although
althoughtype
typeofofccisisCLanguage
CLanguagetype
type
c.print();
c.print();

& [Polymorphism.java]
&
Class Design

q Common fields and methods would be in


super class

q When we define a class, it is important to


design it with hierarchical structure using
inheritance
Class Design
class
classAnsiC
AnsiC{{
int
intdeclarations;
declarations;
int
intoperators;
operators;
int
intstatements;
statements;
void
voidfunctions()
functions(){{
////...... class
}} classOopl
Ooplextends
extendsAnsiC
AnsiC{{
int
intclasses;
classes;
}} int
intexceptions;
exceptions;
class
classJava
Javaextends
extendsAnsiC
AnsiC{{ }}
int
intclasses;
classes;
int
intexceptions;
exceptions; class
classJava
Javaextends
extendsOopl
Oopl{{
int
intthreads;
threads; int
intthreads;
threads;
}} }}
class
classCplusplus
CplusplusExtends
ExtendsAnsiC
AnsiC{{
int
intclasses;
classes; class
classCplusplus
Cplusplusextends
extendsOopl
Oopl{{
int int
intoperatorOverloadings;
intexceptions;
exceptions; operatorOverloadings;
int }}
intoperatorOverloadings;
operatorOverloadings;
}}

You might also like