Professional Documents
Culture Documents
Uploads - Notes - Btech - 5sem - Cse - Java Notes - Inheritance PDF
Uploads - Notes - Btech - 5sem - Cse - Java Notes - Inheritance PDF
Inheritance
Inheritance is the mechanism of deriving new class from old one, old class is knows as
superclass and new class is known as subclass. The subclass inherits all of its instances
variables and methods defined by the superclass and it also adds its own unique
elements. Thus we can say that subclass are specialized version of superclass.
1. Reusability of code
2. Code Sharing
3. Consistency in using an interface
Classes
Base Class
Child Class
B C
Variable “i” with no access modifier and we are using it in child class.
Program runs with no error in this case as members with default
access modifier can be used in child class
Output
Output:
super Keyword:
super is a reference variable that is used to refer immediate parent class object. Uses
of super keyword are as follows:
Example:
Output:
Output:
Output:
Method Overriding
If a class inherits a method from its super class, then there is a chance to override the
method provided that it is not marked final. The benefit of overriding is: ability to
define a behavior that's specific to the subclass type which means a subclass can
implement a parent class method based on its requirement. In object-oriented terms,
overriding means to override the functionality of an existing method.
Ouput:
Output:
Final Keyword
Final keyword can be used in the following ways:
Output:
In this example an
attempt to override a final
method (meth1()) is made
which results in error
Output
Output
Example
Output
Abstract Classes
When the keyword abstract appears in a class definition, it means that zero or more of
its methods are abstract.
Example
Output
1
JAVA NOTES BY ACHIN JAIN
0
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
Interface
An interface cannot contain instance fields. The only fields that can appear in an
interface must be declared both static and final.
As you can see in the above example method is not declared as abstract explicitly and
similarly variable is not declared as static final.
1
JAVA NOTES BY ACHIN JAIN
1
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
But when Java compiles the code, method is declared as abstract implicitly by compiler
and variables are declared as static final.
Output
Case: When definition of the method declared in interface is not provided in the
inherited class. In this case an error will be shown during compile time of the program.
Output:
1
JAVA NOTES BY ACHIN JAIN
2
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
Output
In the above example, two interfaces are created with names “emp” and “director” and
both interfaces contain same method salary(). In the class interfacetest we have
implemented both the interfaces. Now as per the definition of interface a method
1
JAVA NOTES BY ACHIN JAIN
3
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
declared must be overridden in the class that implements the interface. Although in
the above example there are two salary methods declared in different interfaces, but
in the child class there is only single instance of the method. So whenever a call is made
to the salary method it is always the overridden method that gets invoked, leaving no
room for ambiguity.
Package
Packages are used in Java in order to prevent naming conflicts, to control access, to
make searching/locating and usage of classes, interfaces, enumerations and
annotations easier, etc. A Package can be defined as a grouping of related types
(classes, interfaces, enumerations and annotations) providing access protection and
name space management.
Programmers can define their own packages to bundle group of classes/interfaces, etc.
It is a good practice to group related classes implemented by you so that a programmer
can easily determine that the classes, interfaces, enumerations, annotations are
related. Since the package creates a new namespace there won't be any name conflicts
with names in other packages. Using packages, it is easier to provide access control
and it is also easier to locate the related classes.
When creating a package, you should choose a name for the package and put
a package statement with that name at the top of every source file that contains the
classes, interfaces that you want to include in the package. The package statement
should be the first line in the source file.
In the example a package with name “achin_java” is created with statement
“package achin_java;”.
1
JAVA NOTES BY ACHIN JAIN
4
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
Output:
In the code of package “achin_java” four different types of variables are declared.
Variable ‘b’ is declared as public. See the following code in which we have assigned a
value to ‘b’ in class packtest.
1
JAVA NOTES BY ACHIN JAIN
5
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
Output
Case 2: Trying to access a variable with default access modifier outside the package.
Variable ‘a’ is declared as public. See the following code in which we have assigned a
value to ‘a’ in class packtest. You will see an error as shown in the output
Output:
Case 3: Trying to access a variable with private access modifier outside the package.
This is clear case in which you don’t have permission to access private variable outside
its scope.
Case 4: Trying to access a variable with protected access modifier outside the package.
In this there are two different cases. One is trying to access the protected variable
outside the package in the same way as described above for other cases. Other case if
we are inheriting a class defined in a package and then trying to use the protected
declared member.
1
JAVA NOTES BY ACHIN JAIN
6
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
Output:
Output
1
JAVA NOTES BY ACHIN JAIN
7