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

CH 2

The document covers key concepts in Java programming, focusing on inheritance, interfaces, and packages. It explains various types of inheritance, method overriding, the use of the 'super' keyword, and the definition and implementation of interfaces. Additionally, it discusses the creation and organization of packages to manage class namespaces and prevent naming collisions.

Uploaded by

hsg8656
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 views47 pages

CH 2

The document covers key concepts in Java programming, focusing on inheritance, interfaces, and packages. It explains various types of inheritance, method overriding, the use of the 'super' keyword, and the definition and implementation of interfaces. Additionally, it discusses the creation and organization of packages to manage class namespaces and prevent naming collisions.

Uploaded by

hsg8656
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
You are on page 1/ 47

: Inheritance, Interfaces &

Ch 2
Package
Topics Covered:
1. Inheritance: concept of inheritance, type of Inheritance: single inheritance,
multilevel inheritance, hierarchical inheritance, method overriding, final
variables, final methods, use of super, abstract methods and classes.
2.Interfaces: Define interface, implementing interface, accessing interface
variable and methods, extending interfaces.
3. Package: Define packages, types of package, naming and creating package,
accessing package, import statement, static import, adding class and interfaces
to a package.
Total Marks – Min 12 marks.
Inheritance: concept of
Topic 1 :
inheritance
Inheritance
-Inheritance is a mechanism in Java by which derived class can borrow the properties of base class and at the same time the
derived class may have some additional properties.
-The inheritance can be achieved by incorporating the definition of one class into another using the keyword extends.
-Features:
1) It helps in reduced code.
2) It makes use of reusability of code.
3) It enhances readability of code.
4) Execution of code is efficient.
Inheritance: concept of
Topic 1 :
inheritance
Concept of Base class and Derived class
-The inheritance is a mechanism in which the child class is derived from a parent class.
-This derivation is using the keyword extends.
-The parent class is called base class and child class is called derived class.
class A
{
..
..
}
class B extends A
{
..
..
}
Topic 1 : Inheritance: type of Inheritance
Types of Inheritance

1) Single Inheritance

2) Multilevel Inheritance

3) Hierarchical Inheritance
Topic 1 : Inheritance: type of Inheritance
Single Inheritance
-Single level inheritance is that in which there is only one base class and one derived class.
class baseclass
{
body
}
class derivedclass extends baseclass
{
body
}
Topic 1 : Inheritance: type of Inheritance
Single Inheritance
Topic 1 : Inheritance: type of Inheritance
Single Inheritance
Topic 1 : Inheritance: type of Inheritance
Multilevel Inheritance
-It is the mechanism of deriving a derived class from a base class and again a derived class is derived from this
derived class which acts as a base class for newly derived class.

class baseclass
{
body
}
class derivedclass1 extends baseclass
{
body
}
class derivedclass2 extends derivedclass1
{
body
}
Topic 1 : Inheritance: type of Inheritance
Multilevel Inheritance
Topic 1 : Inheritance: type of Inheritance
Multilevel Inheritance
Topic 1 : Inheritance: type of Inheritance
Hierarchical Inheritance
-Hierarchical inheritance is one in which there is only one base class and many derived classes.
class A
{
}
class B extends A
{
}
class C extends A
{
}
class D extends A
{
}
Topic 1 : Inheritance: type of Inheritance
Hierarchical Inheritance
Topic 1 : Inheritance: type of Inheritance
Hierarchical Inheritance
Topic 1 : Inheritance: method overriding
-In a class hierarchy, when a method in a subclass has same name & type as method in a superclass then method
in a sub class is said to be overriding the method in a super class.
-When an overridden method is called within a sub class it will always refer to the method define by the sub class
and method in a superclass will be hidden.
-Method overriding is a key concept in Java that enables Run-time polymorphism.
- It allows a subclass to provide its specific implementation for a method inherited from its parent class.
- The actual method executed is determined by the object’s runtime type, not just the reference variable’s type.
Topic 1 : Inheritance: use of super
-Super is a keyword used to access the immediate super class from sub class.
-There are three ways which the keyword super is used.
1) To access the data members of the immediate super class.
2) To access the class methods of the immediate super class.
3) To access the constructor of the immediate super class.
Topic 1 : Inheritance: method overriding, use of
super
-To access the class methods of the immediate super class.
Topic 1 : Inheritance: method overriding, use of
super
-To access the class methods of the immediate super class.
Topic 1 : Inheritance: method overriding, use of
super
- To access the constructor of the immediate super class
Topic 1 : Inheritance: method overriding, final
variables
-A variable can be declared as final.
-The final variable cannot be modified further.
Topic 1 : Inheritance: method overriding, final
method
-A method can be declared as final.
-The final method cannot be overridden.
Inheritance: abstract methods and
Topic 1 :
classes
- In java abstraction can be achieved using abstract class and abstract methods.
- Sometimes we require just method declaration in super-classes. This can be achieved by specifying the Java abstract type
modifier.
- The abstract Method is used for creating blueprints for classes or interfaces.
- Here methods are defined but these methods don’t provide the implementation.
- Abstract Methods can only be implemented using subclasses or classes that implement the interfaces.
- These methods are sometimes referred to as subclass responsibility because they have no implementation specified in the
super-class.
- Thus, a subclass must override them to provide a method definition.
- If a non-abstract class extends an abstract class, then the class must implement all the abstract methods of the abstract class
- Any class that contains one or more abstract methods must also be declared abstract.
- Thus we can not instantiate the abstract class.(Can not create a object of abstract class)
Inheritance: abstract methods and
Topic 1 :
classes
Topic 1 : Inheritance: static members
-The static members can be static data members and static methods.
-The static members are those members which can be accessed without using object.
-Static member are always preceded by keyword static.
-Static members are called using classname.
-The members that are declaring as static are associated with class itself rather than individual object.
-Static block code execute only once when class is loaded.
-Static members are independent of object. So they can be called before creation of object of class.
Topic 1 : Inheritance: static members
Topic 2 : Interfaces: Define interface
Interfaces
-An Interface in Java programming language is defined as an abstract type used to specify the behavior of a class.
-An interface in Java is a blueprint of a behavior. A Java interface contains static constants and abstract methods.
-By default, variables in an interface are public, static, and final.
-It is used to achieve abstraction and multiple inheritances in Java.
-Interfaces primarily define methods that other classes must implement.
Topic 2 : Interfaces: Define interface
Syntax to define interface
access_modifier interface name_of_interface
{
static final variable_name=value;
..
..
return_type method_name(parameter1,….);
..
..
}
Interfaces: implementing
Topic 2 :
interface
Syntax to implement interface
-It is necessary to create a class for every interface.
-The class must be defined in the following form while using interface.
-we can achieve multiple inheritance by using interface as:
class class_name extends superclass_name implements interface_name1,interface_name2…
{
// body of the class
}
-The methods in interfaces must be defined in the implementing class.
Interfaces: implementing
Topic 2 :
interface
Create interface in i.java file as:

Create another file AB.java as:


Interfaces: implementing
Topic 2 :
interface
Create another file AB.java as:
Interfaces: accessing interface
Topic 2 :
variables
Interfaces: accessing interface
Topic 2 :
methods
Interfaces: accessing interface
Topic 2 :
methods
Topic 2 : Interfaces: extending interface
-When one interface inherits another interface then they uses keyword extends.
-When interface are extends to another interface then this sub interface can’t define the method declare in a super
interface.
-Therefore it is the responsibility of another class that implements interface to define the method of the interface.
Syntax:-
interface A
{
//body
}
interface B extends A
{
//body
}
class C implements B
{
//body
}
Topic 2 : Interfaces: extending interface
Topic 3 : Package: Define packages
Package
- Package are the container for classes that are used to keep class namespace compartment wise.
-Package is very similar to directory where two different directory may have number of files which have same
name..
-Package are used for grouping variety of class and interface together.
-Need of Packages:
-As all the java program are compiled and store in the namespace i.e. default.
-As one namespace should not have more than one class with nickname but when we want to create more than
one class which having the same name than we have to store it in different namespace, so that there should not
be a name collision. For this purpose java uses this concept of package.
-Another use of package is that suppose if we want to use classes from other program without actually copying
them into program then it can be achieved by using package.
Topic 3 : Package: Types of package
Types of package
- Java API provides the large number of classes which are grouped together into different packages according to
their functionality & use for specific application like looping, displaying GUI etc.
Topic 3 : Package: Types of package
i) java.lang:-
This is the language support classes. There are the classes that Java compiler itself uses and therefore they are
automatically imported. They include classes for primitive types, strings, math, functions threads and exceptions.
ii) java.util:-
This is the language utility classes such as vectors, hash tables, random numbers, data etc.
iii) java.io:-
This is the input/output support classes. They provide facility input/output of data and files. They include classes
such as input string, output string, reader, writer.
iv) java.awt:-
It contain the set of classes for implementing graphical user interface. They include classes for window, buttons,
lists, menus and so on.
Topic 3 : Package: Types of package
v) java.net:-
It contains the classes for networking they include classes for communicating with local computers as well as
with internet servers.
vi)java.applet:-
It contains the classes for creating and implementing applets.
Package: naming and creating
Topic 3 :
package
-To create a package simply include a package command as a first statement in a java source file.
Syntax:-
package package_name ;

-To define a class in package , declare as a public.


package p1;
public class A
{
// BODY
}
Package: naming and creating
Topic 3 :
package
-After declaring a class , create a sub directory p1 and compile created java file to obtain .class file and store in
a same directory
-When package having more than one class is to be declare, make class public and store this source file in that
class_name [.java] extension.
-Compile and store .class file in the same directory.
Package: accessing package,
Topic 3 :
import statement
-Save the source file by name AB.java outside the directory p1 and compile it.
Package: adding classes to
Topic 3 :
packages
Package: adding classes to
Topic 3 :
packages
Package: adding multiple
Topic 3 :
packages
Package: adding interface to
Topic 3 :
packages

You might also like