java5com
java5com
Java enables a class to inherit properties and actions from another class called as
inheritance. Through inheritance, a subclass can access members of its superclass (fields and
methods),
o Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called
a derived class, extended class, or child class.
o Super Class/Parent Class: Superclass is the class from where a subclass inherits the
features. It is also called a base class or a parent class.
The extends keyword indicates that we are making a new class that derives from an existing
class. The meaning of "extends" is to increase the functionality.
class Employee{
float salary=40000; }
class Programmer extends Employee{
int bonus=10000;
main(String args[]){
Output:
In Java programming, multiple and hybrid inheritance is supported through interface only.
1.Single Inheritance
In the example given below, Dog class inherits the Animal class, so there is the single
inheritance.
Example
eat() {
System.out.println("eating...");
bark(){
System.out.println("barking...");
Dog();
d.bark();
d.eat();
Output:
barking...
eating...
2.Multilevel Inheritance
As you can see in the example given below, BabyDog class inherits the Dog class which again
inherits the Animal class, so there is a multilevel inheritance.
eat(){
System.out.println("eating...");
bark(){
System.out.println("barking...");
void weep(){
System.out.println("weeping...");
d=new BabyDog();
d.weep();
d.bark();
d.eat();
Output:
weeping...
barking...
eating...
3.Hierarchical Inheritance
When two or more classes inherits a single class, it is known as hierarchical inheritance.
In the example given below, Dog and Cat classes inherits the Animal class, so there is
hierarchical inheritance.
Example
{System.out.println("eating...");}
{System.out.println("barking...");}
{System.out.println("meowing...");}
}
Cat();
c.meow();
c.eat();
c.bark();
Output:
meowing...
eating...
4.Multiple Inheritance
Multiple Inheritance A class's capacity to inherit traits from several classes is referred to as
multiple inheritances.
Multiple inheritances, however, can result in issues like the diamond problem, which occurs
when two superclasses share the same method or field and causes conflicts. Java uses
interfaces to implement multiple inheritances in order to prevent these conflicts.
Example
interface Character {
void attack();
}
interface Weapon {
void use();
wand.
}
}
Output:
Java supports multiple inheritance through interfaces only, where a class can implement
multiple interfaces. Multiple inheritance in Java is not possible by class, but it is possible
through interfaces.
The hybrid inheritance is the composition of two or more types of inheritance. The main
purpose of using hybrid inheritance is to modularize the code into well-defined classes. It
also provides the code reusability.
Example:
class C
System.out.println("C");
class A extends C
System.out.println("A");
class B extends C
System.out.println("B");
{
System.out.println("D");
Output:
An abstract class in Java acts as a partially implemented class that itself cannot be
instantiated.
It exists only for subclassing purposes, and provides a template for its subcategories to
follow.
Abstract methods are declared to have no body, leaving their implementation to subclasses.
Abstract Method in Java
A method which is declared as abstract and does not have implementation is known as an
abstract method.
Example
{System.out.println("running safely");}
obj.run();
Output:
running safely
An abstract class can have a data member, abstract method, method body (non-abstract
method), constructor, and even main() method. Example
abstract class Bike{
Bike(){System.out.println("bike is created");}
{System.out.println("gear changed");}
Output:
bike is created
running safely..
gear changed
Abstract Methods: Abstract classes can have abstract methods, which are declared without
a body. Subclasses must provide concrete implementations for these methods.
Concrete Methods: Abstract classes can also contain concrete methods with defined
behavior. Subclasses inherit these methods along with their implementations.
Interface in Java
• There can be only abstract methods in the Java interface, not a method body.
Why use a Java interface?
There are mainly three reasons to use an interface. They are given below.
Syntax:
interface interface_name{
// by default.
Declaring Interface
interface Animal {
sleep();
}
//Creating an interface
interface Printable{
void print();
void print(){System.out.println("Hello");}
p.print();
Output:
Hello
Example
interface Printable{
void print();
interface Showable{
void show();
c.print();
c.show();
Output:
printing data...
showing data...
Inheritance of Interfaces
A class implements an interface, but one interface can extend another interface.
Example
print();
show();
show(){System.out.println("Welcome");}
obj.print(); obj.show();
Output:
Hello
Welcome
Abstract class and interface both are used to achieve abstraction where we can declare the
abstract methods.
1) Abstract class can have abstract and Interface can have only abstract methods. Since
nonabstract methods. Java 8, it can have default and static methods
also.
3) Abstract class can have final, non-final, static Interface has only static and final variables.
and non-static variables.
4) Abstract class can provide the implementation Interface can't provide the implementation of
of interface. abstract class.
5) The abstract keyword is used to declare The interface keyword is used to declare
abstract class. interface.
6) An abstract class can extend another Java class An interface can extend another Java interface
and implement multiple Java interfaces. only.
7) An abstract class can be extended using An interface can be implemented using keyword
keyword "extends". "implements".
8) A Java abstract class can have class members Members of a Java interface are public by
like private, protected, etc. default.
9)Example: Example: public interface
public abstract class Shape{ public Drawable{ void draw();
abstract void draw(); }
}