CH 3 Inheritance, Package and Collection
CH 3 Inheritance, Package and Collection
It is the mechanism in java by which one class is allowed to inherit the features like
fields and methods of another class.
It is a technique which helps to reduce number of coding lines in the program and
increases the program development speed.
Inheritance represents the IS-A relationship which is also known as a parent-child
relationship.
Java does not support multiple inheritance because it creates the complexity in
coding.
So the java has provided is interface to support the concept of multiple inheritance.
Concept of inheritance: The concept of inheritance is to create new classes that are
built upon existing classes. When you inherit from an existing class, you can reuse
methods and fields of the parent class.
Inheritance can be achieved using extends keyword.
The class being inherited is called as super class or base class or parent class.
The class which inherits the properties of other is called as sub class or derived class or child
class.
The main advantage of inheritance is code reusability.
Syntax :
class Vehicle
{
…………….
……………
}
class Car extends Vehicle
{
………….
……………
}
Types of Inheritance
i) Single inheritance ii) Multilevel Inheritance iii)Hierarchical Inheritance
Program : MultilevelInh.java
Inheritance in Constructor: In inheritance the constructor never get inherited to any
child class.
-Constructor of sub class is invoked when we create the object of sub class, it by
default it invokes the default constructor of super class.
-Hence ,in inheritance the objects are constructed top-down.
-When we cerate an object of the child class , the parent class constructor is
executed , followed by the child class constructor.
-Prog ConstInherit.java
- Syntax :
Interface interface-name
{
// abstract methods
}
Creation and implementation of an interface ,interface reference:-
-to declare interface , the interface keyword is used.
-Ex :- interface shape
{
static final float pi=3.14;
float--------------
}
-in above program interface keyword is used to declare an interface.
-the name of interface is shape.
Implementation of an interface:- interface can be used as super classes, whose
properties are inherited by classes .
-Once an interface has been defined ,one or more classes can implement that interface
- To implement or achieve an interface class, use the keyword implements.
-Syntax : Access_Specifier class classname implements interfacename
{
Body of class
}
Interface Reference :- access specifier class classname extends super class implements
interface1, interface2,……………………{
{
Body of class
}
- Example InterfaceDemo1.java
Interface Inheritance:- An interface can extended another interface in the same way that a
class can extend another class.
- The extends keyword is used to extend an interface , and the child interface inherits the
methods of the parent interface. Interface A
- Diagram:
- Ex : InterfaceExtends.java
Extends Implements
By using “extends” keywords a class can inherit By using “implements “ keywords a class can
another class or an interface can inherit other implement an interface.
interfaces.
It is compulsory that sub class that extends a It is compulsory that class that implementing an
super class overrides all the methods in a super interface has to implement all the methods of
class. that interface.
Only one super class can be extended by a class. A class can implement any number of an
interface at a time.
Any number of interface can be extended by An interface can never implements any other
interface. interface.
Dynamic Method Dispatch:-
-When Parent class reference variable refers to child class object ,it is known as
upcasting .
-It is the mechanism in which java resolve calls to overridden method during run time
not during compile time .this concept is called dynamic linking or runtime
polymorphism.
- In java , this can be done and is helpful in situation where multiple child classes
extends single parent class.
- In such situation , we can create a parent class reference and assign child class
objects to it.
Parent
Reference
variable of Object of
parent class child class
- Child extends
Package:-
-Packages Concept:- A java package is a group of similar type of classes , interface and
sub-packages.
-Package in java is a mechanism to encapsulate a group of classes , sub-packages and
interfaces.
- It is similar to the class libraries which provide the reusability.
- Packages help us to write better and manageable code by preventing naming
conflicts.
- Software written in the java programming can be composed of 100 or 1000 of
individuals classes, it makes sense to keep things organized by placing related classes
and interface into packages.
- Advantages of java packages:-
i. Re-usability:- the classes contained in the package of another program can be easily
reused.
ii. Name conflicts:- packages help us to uniquely identify a class .
Ex: company.sales.Employee and company.marketing.Empolyee classes.
iii. Controlled access :- Offers access protection such as protected classes, default
classes and private classes.
iv. Data Encapsulation:- They provide a way to hide classes , preventing other programs
from accessing classes that are meant for internal use only.
v. Maintenance:- with packages you can organize your project better.
c. java.io :- it has stream classes for I/O .They provide the facility for input and output of
data.
d. java.awt :- classes for implementing graphical user interface- windows, button, menus etc.
e. java.net :- classes for networking .they include classes for communicating with local
computers as well as with internet servers.
Program : i) Addition.java
ii) Multiplication.java
iii)PackageDemo.java
Collection:-
-Java collection are set of pre-defined classes and interfaces that help programmer to perform
different kind of data structure operations like sorting ,searching,traversing,storing processing data
effectively.
- The collection interface (java.util.Collection) and MAP interface (java.util.MAP) are the two main
“root” interface of java collection classes.
Collection Framework:- In java, a separate framework named the “Collection Framework” holds all
the collection classes and interface in it.
-all collection frameworks contain the following:-
i. Interfaces: these are abstract data types that represent collections.
- Interface allow collections to be manipulated independently of the details of their representation.
ii. Implementation(classes) :- These are the concrete implementation of the collection interface.
In essence they are reusable data structure.
iii. Algorithms:- these are the methods that perform useful computations such as searching and
sorting , on objects that implements collection interfaces.
- The same method can be used on many different implementations of the appropriate collection
interface.
Hierarchy of the collection framework:- The utility package (java.util) contains all
the classes and interface that are required by the collection framework.
-This interface is extended by the main collection interface which acts as a root for the
collection framework .
-All the collections extended this collection interface thereby extending the properties
of the iterator and the methods of this interface.
-Program ArrayListDemo1.java
Interface :Collection ,List ,Set :-
-All collection classes must implements the collection interface. The collection interface
defines some methods which enable us to access the objects of a collection.
-this interface contains various methods which can be directly used by all the collections
which implements this interface.
-This interface extends the iterable interface and is implemented by all the classes in the
collection framework.
-this interface contains all the basic methods which every collection has like adding the
data into the collection ,removing the data, clearing the data.
The collection classes:- Java provides a set of standard collection that implement
collection interfaces.
List interface:- the list interface extends the collection interface.
-it represents an ordered or sequential collection of objects.
-the classes which implements the List interface are called as Lists.
- The list interface declares the behavior of a collection that stores a sequence of elements.
- Lists are classified into following 3 types:
i. ArrayList
ii. LinkedList
iii. Vectors.
Program :