Java lec 5
Java lec 5
Inheritance is the mechanism in Java by which one class can inherit the
features from another class.
A class that inherits the features of another is known as child class/
subclass/ derived class and the class which is being inherited is known as
superclass/ parent class/ base class.
A subclass inherits all the properties and methods of the super class, and
can have additional attributes and methods.
Types of Inheritance
There are five different types of inheritance:
1) Single inheritance
2) Multilevel inheritance
3) Multiple inheritance
4) Hierarchical inheritance
5) Hybrid inheritance
1. Single inheritance:
2. Multilevel inheritance:
When there is a chain of inheritance, it is known as multilevel
inheritance.
In the below figure, class A serves as a base class for the derived class B,
which in turn serves as a base class for the derived class C. In Java, a
class cannot directly access the grandparent’s members.
3. Multiple inheritance:
In multiple inheritance, one class can have more than one superclass
Java does not support multiple inheritances with classes. In Java,
multiple inheritances can be achieved only through Interfaces.
4. Hierarchical inheritance:
Overriding Method
If subclass (child class) has the same method as declared in the parent
class, it is known as method overriding in Java.
In other words, when a method in a subclass has the same name, the
same parameters or signature, and the same return type as a method in
its super-class, then the method in the subclass is said to override the
method in the super-class.
When an overridden method is called from within a subclass, it will
always refer to the version of the method defined by the subclass. The
superclass version of the method is hidden.
A superclass reference variable can be assigned a subclass object. This is
illustrated by the following code:
package First_Package;
class super_class {
int i = 0;
void doOverride (int k) {
i = k;
}
}
class sub_class extends super_class {
void doOverride(int k)
{
i = 2 * k;
System.out.println("The value of i is: " +i);
}
}
public class new_class {
public static void main(String[] args) {
}
}
super Keyword:
package First_Package;
class super_class
{
int b = 30;
}
package First_Package;
class super_class
{
void show()
{
System.out.println("superclass class method");
}
}
void show()
{
super.show();
System.out.println("subclass class method");
}
Example:
package First_Package;
class super_class2
{
super_class2(String name)
{
System.out.println("Super Class Constructor : " + name );
}
}
sub_class2(String name)
{
super(name);
System.out.println("Sub Class Constructor : " + name );
}
final Keyword:
Example:
package First_Package;
class super_class
{
final int MAX = 100;
final void show(final int x)
{
// MAX++; illegal statement as MAX is final
// x++; illegal statement as x argument is final
System.out.println("Superclass show method: " +x);
}
}
public class sub_class extends super_class {
Abstract Class:
// Abstract class
abstract class Animal {
// Abstract method
abstract void sound();
// Non-abstract method
void eat() {
System.out.println("The animal eats.");
}
}
// Concrete subclass
class Dog extends Animal {
// Implement the abstract method
void sound() {
System.out.println("The dog barks.");
}
}
Interfaces
Variables in Interface:
Packages
Package in java is a group of similar types of classes, interfaces and sub-
packages.
1. Built-in Packages
These packages consist of a large number of classes which are a part of
Java.Some of the commonly used built-in packages are:
2. User-defined Packages
These are the packages that are defined by the user.
2. Code Reusability
Once a package is created, it can be reused in multiple projects.
Encourages modular programming by grouping related classes together.
Creating Packages
To create a package, use the package keyword at the beginning of a Java
file.
Using Packages
In Java, you can import classes from a package using either of the
following methods:
1. Import a specific class:
import mypackage.MyClass;
Access Protection