0% found this document useful (0 votes)
5 views17 pages

Inheritance and Polymorphism Explained

Uploaded by

Central Hùng
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views17 pages

Inheritance and Polymorphism Explained

Uploaded by

Central Hùng
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Inheritance and

Polymorphism
QUYENNV
Superclass and Subclass
Inheritance enables you to define a general class (i.e., a superclass) and
later extend it to more specialized classes (i.e., subclasses)
Superclass and Subclass
In any case, constructing an instance of a class invokes the
constructors of all the super-classes along the inheritance chain.
When constructing an object of a subclass, the subclass
constructor first invokes its superclass constructor before
performing its own tasks. If the superclass is derived from another
class, the superclass constructor invokes its parent-class
constructor before performing its own tasks. This process
continues until the last constructor along the inheritance hierarchy
is called. super() may be used to override the method
Polymorphism
Polymorphism means that a variable of a supertype can refer to a subtype
object.
Instanceof and equal()
Arraylist class
An ArrayList object can be used to store a list of objects
Arraylist class
Arraylist class
Useful methods for lists
Data and method visibility
Preventing extending and
overriding
You may occasionally want to prevent classes from being extended. In
such cases, use the final modifier to indicate that a class is final and
cannot be a parent class. The Math class is a final class. The String,
StringBuilder, and StringBuffer classes are also final classes. For
example, the following class A is final and cannot be extended:
public final class A {
// Data fields, constructors, and methods omitted
}
public class Test {
// Data fields, constructors, and methods omitted
public final void m() {
// Do something
}
}
Exercise I
Exercise I
Exercise I
Exercise II
Exercise III
Exercise IV

You might also like