Java_log2000_Classes and Objects 4

Keywords: Java Attribute JDK

Class Eny2

Keywords: Inheritance; Object class; Polymorphism;

inherit

  • Through the extends keyword;
  • Subclasses can inherit all non-private ly owned properties and methods from their parent class as members.

When a subclass inherits a parent class, it should first satisfy that the parent class is accessible, for example, when the subclass and the parent do not
In the same package, the parent modifier must be public; if the parent can be accessed, the
Is a parent attribute member or method with modifier public or protected that can be accessed by a child class;
private property members or methods cannot be accessed.

Object class

  • The Object class is the common ancestor of all classes, even if the extents Object was not written when the class was defined.
  • There are many methods defined in Object that can be inherited by all subclasses.

Final Class

A final-modified class is the final class and cannot have subclasses.

polymorphic

Definition of polymorphism

  • The concept of polymorphism: polymorphism refers to the coexistence of different methods with the same name in a program
  • There are two mechanisms of polymorphism in Java: overloading and overriding.

Overload example 1

  class Parent{
     public int getScore(){
         return 3;
     }
     public int getScore(int i){
         return i;
     }
  }

There are two getScore methods in the Parent class with different parameters

Overload Example 2
For example: class Applet

- -
AudioClip getAudioClip(URL url) Obtains AudioClip objects from URLs
AudioClip getAudioClip(URL url, String name) obtains AudioClip objects from URLs that are joined by URLs and names
public AudioClip getAudioClip(URL url) {
      . . . . . . . ;
 }
 public AudioClip getAudioClip(URL url, String name) {
        try {
        //Functionality is achieved by reconstructing a new URL object and then calling the same-name method above.
    return getAudioClip(new URL(url, name));
        } catch (MalformedURLException e) {
        return null;
        }
 }

Overload example 3
Example: java.util.Class LinkedList

- -
Boolean addAll(Collection c) Inserts a collection object into the end of the list
Boolean addAll(int index, Collection c) inserts a collection object into a specified location in the chain table.
public boolean addAll(Collection c) {
   return addAll(size, c); 
   //size is the length of the list. This method calls the following method to insert the incoming object at the end of the specified list
}
//Index is the current index number of the chain table
public boolean addAll(int index, Collection c) {    
       ......
} 

cover

Method with the same name as the parent (method name is the same, parameter is the same, return)
Same type) redefined, i.e. defined in a subclass and determined in a parent class
Methods that have the same name but different contents.

  • Method names are the same, parameter names are the same, and return types are the same: override
  • Method names are the same, parameter names are different: overload
  • Method names are the same, parameter names are the same, and return types are different: compilation cannot pass

Coverage example 1

class Parent {
   public int getScore()
   { return 3; }
   public String getCountryName()
   { return "China"; }
}
class Son extends Parent {
    public int getScore(){
      return 4;
    }
 }
 public class RunSon{
    public static void main(String args[]){
      Son  s = new Son();
      System.out.println(s.getScore());
      System.out.println(s.getCountryName()); 
     }
}
//----------------------
//Output results:
4
China

Overlay - Attention

  • Access modifier permissions for subclasses should be equal to or greater than the parent class
  • Static methods cannot override non-static methods, nor can they be overridden by non-static methods, but static methods can override static methods.
  • The method has a final modifier before it and cannot be overridden in a subclass method
  • In JDK, many parent class methods are overridden by subclasses, giving different meanings, such as boolean equals(Object obj) methods in Object classes
  • If abstract methods exist in abstract classes, specific subclasses must override them

Coverage Example 2

class Super {
    public Integer getLenght() { return new Integer(4); }
}
public class Sub extends Super {
    public Long GetLenght() { return new Long(5); }
    public static void main(String[] args) {
         Super sooper = new Super();
         Sub sub = new Sub();
         System.out.println(
         sooper.getLenght().toString() + "," +
         sub.getLenght().toString() );
     }
 }
//-------------
//Output results:
44

visitor tracker

Posted by semtex on Mon, 24 Jun 2019 19:20:28 -0700