CH 2
CH 2
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: