Java - Inheritance
subclass - inherit
-A subclass inherits all the members (fields, methods, and nested classes) from its superclass. -Constructors are not members, so they are not inherited by subclasses -the constructor of the superclass can be invoked from the subclass.
subclass
-The class which inherits the properties of other -(derived class, child class)
HAS-A relationship
-are mainly based on the usage -determines whether a certain class HAS-A certain thing. -helps to reduce duplication of code as well as bugs.
Superclass reference variable
-can hold the subclass object -but using that variable you can access only the members of the superclass -access the members of both classes it is recommended to always create reference variable to the subclass.
Invoking Superclass Constructor
-f a class is inheriting the properties of another class, the subclass automatically acquires the default constructor of the superclass. -But if you want to call a parameterized constructor of the superclass, you need to use the super keyword as shown below. super(values);
super
-similar to this keyword Following are the scenarios where the super keyword is used. -used to differentiate the members of superclass from the members of subclass, if they have same names. -used to invoke the superclass constructor from subclass.
superclass
-the class whose properties are inherited -(base class, parent class)
super : differentiating the Members
If a class is inheriting the properties of another class. And if the members of the superclass have the names same as the sub class, to differentiate these variables we use super keyword as shown below. super.variable super.method();
extends syntax
class Super { ..... ..... } class Sub extends Super { ..... ..... }
Invoking Superclass Constructor : Invoking Superclass Constructor
class Superclass { int age; Superclass(int age) { this.age = age; } public void getAge() { System.out.println("The value of the variable named age in super class is: " +age); } } public class Subclass extends Superclass { Subclass(int age) { super(age); } public static void main(String argd[]) { Subclass s = new Subclass(24); s.getAge(); } }
a class can implement one or more interfaces
helped Java get rid of the impossibility of multiple inheritance.
The instanceof Keyword : Example
interface Animal{} class Mammal implements Animal{} public class Dog extends Mammal { public static void main(String args[]) { Mammal m = new Mammal(); Dog d = new Dog(); System.out.println(m instanceof Animal); System.out.println(d instanceof Mammal); System.out.println(d instanceof Animal); } } output : true true true
IS-A Relationship
is a way of saying: -This object is a type of that object
extends
is the keyword used to inherit the properties of a class
single inheritance B -> A
public class A { ..... } public class B extends A { ..... }
hierarchical inheritance: B ->A C->A
public class A { ..... } public class B extends A { ..... } public class C extends A { ..... }
multi level inheritance C -> B -> A
public class A { ..... } public class B extends A { ..... } public class C extends B { ..... }
multiple inheritance C ->A C->B
public class A { ..... } public class B { ..... } public class C extends A,B{ ...} ***A very important fact to remember is that Java does not support multiple inheritance. This means that a class cannot extend more than one class.
IS-A Relationship : extends keyword is used to achieve inheritance. - Example
public class Animal { } public class Mammal extends Animal { } public class Reptile extends Animal { } public class Dog extends Mammal { }
HAS-A relationship : example
public class Vehicle{} public class Speed{} public class Van extends Vehicle { private Speed sp; } This shows that class Van HAS-A Speed. By having a separate class for Speed, we do not have to put the entire code that belongs to speed inside the Van class, which makes it possible to reuse the Speed class in multiple applications.
IS-A Relationship : implements keyword - Example
public interface Animal { } public class Mammal implements Animal { } public class Dog extends Mammal { }
Types of Inheritance
single inheritance multi level inheritance hierarchical inheritance multiple inheritance
Inheritance
the process where one class acquires the properties (methods and fields) of another. -manageable in a hierarchical order.
IS-A Relationship : extends keyword
the subclasses will be able to inherit all the properties of the superclass except for the private properties of the superclass.
The instanceof Keyword
to check determine whether Mammal is actually an Animal, and dog is actually an Animal.
IS-A Relationship : implements keyword
used with classes to inherit the properties of an interface. Interfaces can never be extended by a class.