What is Inheritance in java - OOPS principles

Inheritance


Inheritance is a process where child class acquires the properties of super class.
OR
Inheritance is a process where features of parent/super class are inherited in child/sub class.


Program 3.1 to show how features of parent/super class are inherited in child/sub class >
Animal is superclass of Lion.
Lion is subclass of Animal.


food() method of Animal class is inherited in Lion class.
class Animal {
   void food() {
          System.out.println("Animal eat food");
   }
}
class Lion extends Animal {
  
}
/** JavaMadeSoEasy.com */
public class MyClass{
   public static void main(String[] args) {
          Lion obj = new Lion();
          obj.food();
   }
}
/*OUTPUT
Animal eat food
*/

Inheritance can be achieved by using following keywords>
  • extends and
  • implements


Class can use extend and implements >
Class always extends another class.
Class always implements interface.
IS-A relationship with Classes>
Animal IS-A Object (because all classes extends java.lang.Object by default)


AquaticAnimal IS-A Object
AquaticAnimal IS-A Animal


Fish IS-A Object
Fish IS-A Animal
Fish IS-A AquaticAnimal


Program 3.2 to demonstrate IS-A relationship with Classes>
class Animal {}
class TerrestrialAnimal extends Animal{}
class AquaticAnimal extends Animal{}
class Lion extends TerrestrialAnimal {}
class Goat extends TerrestrialAnimal {}
class Fish extends AquaticAnimal {}
class Whale extends AquaticAnimal {}
/** JavaMadeSoEasy.com */
public class MyClass{
   public static void main(String[] args) {
          Object a = new Animal();  //Animal IS-A Object

          Object aa1 = new AquaticAnimal(); //AquaticAnimal IS-A Object
          Animal aa2 = new AquaticAnimal(); //AquaticAnimal IS-A Animal
         
          Object f1 = new Fish(); //Fish IS-A Object
          Animal f2 = new Fish(); //Fish IS-A Animal
          AquaticAnimal f3 = new Fish(); //Fish IS-A AquaticAnimal
   }
}


Interface uses extend only >
Interface always extends another interface.
Interface can extend more than one interface.


IS-A relationship with Classes and interfaces>


AquaticAnimal IS-A Object
AquaticAnimal IS-A Animal


Fish IS-A Object
Fish IS-A Animal
Fish IS-A AquaticAnimal

Program 3.3 to demonstrate IS-A relationship with Classes and interfaces>
interface Animal {}
class TerrestrialAnimal implements Animal{}
class AquaticAnimal implements Animal{}
class Lion extends TerrestrialAnimal {}
class Goat extends TerrestrialAnimal {}
class Fish extends AquaticAnimal {}
class Whale extends AquaticAnimal {}
/** JavaMadeSoEasy.com */
public class MyClass{
   public static void main(String[] args) {
          Object aa1 = new AquaticAnimal(); //AquaticAnimal IS-A Object
          Animal aa2 = new AquaticAnimal(); //AquaticAnimal IS-A Animal
         
          Object f1 = new Fish(); //Fish IS-A Object
          Animal f2 = new Fish(); //Fish IS-A Animal
          AquaticAnimal f3 = new Fish(); //Fish IS-A AquaticAnimal
   }
}






RELATED LINKS>

Java tutorial & history of java >

Java tutorial - Advantage, Where is java used in real world, platform independent language



Classes, objects, interface, abstract classes and constructors >

Classes and Objects in java - Class consist of blocks, constructor, variables, methods, inner classes and inner interfaces



Interface in java - Multiple inheritance, Marker interfaces, When to use interface practically, 12 features



Abstract class in java - When to use abstract class or interface practically, 10 features



Access modifiers >

Access modifier /Access specifier in java - private, package-private(Default), protected and public - Diagram and tabular form


Method overloading and overriding >

Method overloading in java - in detail with programs,10 Features,need of method overloading, overloading main method, Diagram and tabular form of Implicit casting/promotion of primitive Data type


Method overriding in java - in detail with programs, 10 Features, need of method overriding, understanding @Override annotation, Covariant return, diagram to understand access modifiers

No comments:

Post a Comment