java inheritance extends

In Java, class inheritance single inheritance, that is, a subclass can have only one parent class

Two most commonly used keywords inheritance is extends and the implements .

These two keywords determine whether an object and another object is IS-A (a) relationship.

By using these two keywords, we can achieve a target to get the properties of another object.

All Java classes are inherited from the java.lang.Object class, so the Object is the ancestor of all classes, and in addition Object, all classes must have a parent class.

You can declare a class via the extends keyword is inherited from another class, the general form is as follows:

// A.java
public class A {
    private int i;
    protected int j;
 
    public void func() {
 
    }
}
 
// B.java
public class B extends A {
}

Described above code fragment, B inherited by A, B is a subclass of A. And A is a subclass of Object, there may not be explicitly declared.

As a subclass instance of B has all the member variables A, but for members of the private variable B did not have access, which guarantees the encapsulation A's.

 

IS-A relationship

That IS-A: An object is a another object classification.

Here is the keyword extends inheritance.

public class Animal{
}

public class Mammal extends Animal{
}

public class Reptile extends Animal{
}

public class Dog extends Mammal{
}

 

Based on the above example, the following statement is true:

  • Animal class is the parent class Mammal class.
  • Animal Reptile class is the parent class.
  • Mammal and Reptile class is a subclass of Animal class.
  • Dog class is both a subclass of the Mammal class is a subclass of Animal class.

Analysis IS-A relationship between the above example, as follows:

  • Mammal IS-A Animal
  • Reptile IS-A Animal
  • Dog IS-A Mammal

Thus: Dog IS-A Animal

By using the keyword the extends , subclasses can inherit all the properties except private property outside the parent class.

We use the instanceof operator can determine Mammal IS-A Animal

 

Introduction to extends after the keyword, we'll look at the implements keyword is used to indicate how to use the IS-A relationship.

Implements keyword in the class inheritance interface to the case, this situation can not use the keyword the extends .

public interface Animal {}

public class Mammal implements Animal{
}

public class Dog extends Mammal{
}

You may be used  instanceof  operator to verify and dog Mammal Animal object is an instance of class.

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);//true
      System.out.println(d instanceof Mammal);//true
    System.out.println(d instanceof Animal);//true
 
 
  }
}
 

HAS-A relationship

Dependencies between HAS-A representatives of the class and its members. This helps reduce errors and code reuse code.

example

public class Vehicle{}
public class Speed{}
public class Van extends Vehicle{
    private Speed sp;
} 

 

Speed ​​and Van class category is HAS-A relationship (Van a Speed), so that the entire code is not adhered to Van Speed ​​class of the class, and class Speed ​​may be reused in multiple applications.

In object-oriented features, the user need not worry about how the internal class implementation.

Generally, we inherit a base class and abstract class with extends keyword, implement interface classes inherit with the implements keyword.

Guess you like

Origin www.cnblogs.com/150536FBB/p/12022613.html