0% found this document useful (0 votes)
27 views

CH 3 Inheritance, Package and Collection

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

CH 3 Inheritance, Package and Collection

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

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

 Multilevel Inheritance – Method Overriding Handle Multilevel Constructor.


-Method overriding is the concept where two or more methods have the same name
and signature as a method in its super class .
-It means sub class method is said to override to the super class method.
-Method overriding means redefining the method in the derived classes( sub class).
-Prog 1) OverridingDemo.java 2)OverridingMethod.java
 Super keyword:
i. Super can be used to refer immediate parent class instance variable.
ii. Super can be used to invoke immediate parent class method.
iii. Super can be used to invoke immediate parent class constructor.

i. Super can be used to refer immediate parent class instance variable:


Super keyword to access the data member or field of parent class.it is used if parent
class and child class have same field names.it hided the super class members and can
access them with super keyword.
Prog SuperVariable.java
ii. Super can be used to invoke immediate parent class method:
It should be used if sub class contains the same method as parent class(i.e. overridden
method).
Prog SuperMethod.java
iii. Super in used to invoke parent class constructor : the super keyword can also be used
To invoke the parent class constructor.
- When an object of child class is created, it automatically calls the parent class default
constructor before its own , but the parameterized constructor of parent class must be
called explicitly using the super keyword inside the child class constructor .
-super keyword can call both parameterized as well as non parametric constructor
depending upon the situation.
- Prog SuperConstr.java
 Final keyword: if we declare a method as final, then it cannot be overridden by any sub
classes.
-If we declare a class as final , we restrict the other classes to inherit or extend it.
-there are 3 implementation of final keyword:
i) final variable :- to create constant: when a variable is declared with final keyword ,
its value can’t be modified , it’s a constant.
Ex:- class FinalVariable
{
public static void main(String args[])
{
final int var = 50;
var=60;
System.out.println(“the value of constant is : ” +var );
}
}
ii) final class: to prevent from inheritance:-
-we can declare a class with a final keyword in java.
-A class declared as a final class ,cannot be sub classed.

iii)final method: to prevent method overriding:-


- A method declared with the final keyword ,cannot be overridden or hidden by sub
classes.
-we declare java method as final method by adding the final keyword before the method
name.
- The method with final keyword cannot be overridden in the sub classes.
 Interface: A Java interface is like a java class ,except a java interface can only contain
method signatures and fields .
-The interface in java is a mechanism to achieve abstraction.
-There can be only abstract methods in java interface , not method body .
-It is used to achieve abstraction and multiple inheritance in java.

- Properties of a java interface:-


i. An interface is implicitly abstract .While declaring an interface , you do not need to
use the keyword abstract.
ii. Each method of an interface is also implicitly abstract , so we need not use the
abstract keyword while declaring methods inside an interface.
iii. Each method in an interface is by default public and abstract.
iv. All variables defined in an interface are by default public ,static , and final .
v. Interface nothing but deals between client and developer.
 Need for interface :-
i. To achieve security – it hides certain details and only show the essential details of
an object.
ii. Java does not support multiple inheritance. It can be achieved with interfaces ,
because the class can implement multiple interfaces.
iii. Interface are used to implement abstraction.

- 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.

Disadvantages :- we can not pass the parameter to the package.

Packages in java can be categorized in two forms:


i. Built in packages
ii. User defined packages.
i. Built in packages :-
a. java.lang :-it contains the language support classes .
it contains classes for primitive types, string , math functions , threads and
exception.
b. java.util :- it contains language utility classes such as vectors, hash tables , dates ,
calendars.

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.

f. java.Applet :- classes for creating and implementing applets.


ii. User Defined Packages:-
a. Package statement must be the first statement in the program even before the
important statement.
b. A package is always defined as a separate folder having the same name as the
package name.
c. Store all the classes in that package folder.
d. All classes of the package which we wish to access outside the package must be
declared public.
e. All classes within the package must have the package statement as its first line.
f. All classes of the package must be compiled before use.

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.

 Methods of collection Interface:- the collection interface defines some methods


which enable us to access the objects of a collection.

 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.

 Set Interface :- the set interface extends the Collection interface


- A Set is a Collection that cannot contain duplicate elements.
- It models the mathematical set abstraction .
- It is an unordered collection or list in which duplicate are not allowed ,is referred as a
collection interface.
- The Set interface is used to create the mathematical set.
- add(), Clear(),contains(), isEmpty(), iterator(), remove(), size().
 Navigation: Enumeration, Iterator, ListIterator
-Enumeration, Iterator, ListIterator are interface in java used to traverse through
collection and access the objects from the collection.
i. Enumeration:- Enumeration can be used for forward navigation only. Element cannot
be removed using Enumeration.
-to create Enumeration object, we have to use ‘elements()’ method of Vector class.

Program :

You might also like