Inheritance, Fields and Methods

  1. Member Fields: These are variables that are declared inside a class, but outside any method, constructor or block. They represent the properties or attributes that objects of that class should have. For example, in the context of an Animal class, a member field could be name, representing the name of the animal.

  2. Member Methods: These are functions declared within a class. They represent the behaviors or actions that objects of the class can perform. For instance, an Animal class may have a member method like eat(), which simulates the action of the animal eating.

  3. Inheritance: This is a feature in Java where one class (known as the subclass or child class) can inherit the member fields and methods of another class (the superclass or parent class). In the case of an Animal and Dog scenario, a Dog class could inherit the member fields and methods of the Animal class, because a dog is a type of animal.

  4. Superclass's Constructor: When creating an instance of the subclass, the constructor of the superclass is also invoked to ensure that the inherited attributes are properly set. This is usually done through a special function called super(), which calls the constructor of the superclass.

  5. Overriding: This is a feature that allows a subclass to provide a different implementation of a method that is already present in its superclass. For example, while there might be a general eat() method in the Animal class, the Dog class might override this method to provide a more specific version of eating, say, eat() that indicates the dog is eating happily.

  6. Adding New Fields and Methods: The subclass isn't restricted to just the inherited fields and methods. It can define its own fields and methods too. For instance, the Dog class could have a breed field and a bark() method that aren't part of the general Animal class.

  7. Accessing Methods: When a method is called on an object, the version of the method in the object's actual class is the one that gets executed. If the Dog class has its own version of eat(), then calling eat() on a Dog object will execute the Dog's eat() method, not the Animal's eat() method.

  8. Visibility: In Java, not all members of a superclass are inherited by subclasses. If a member of a superclass is declared as private, it cannot be accessed directly by the subclass. It is only accessible through methods in the superclass. To make a member field or method accessible to a subclass, it should be declared as protected or public in the superclass.


  • Members of the superclass that are marked private:

    • are not inherited by the subclass,

    • exist in memory when the object of the subclass is created

    • may only be accessed from the subclass by public methods of the superclass.

  • Members of the superclass that are marked public:

    • are inherited by the subclass, and

    • may be directly accessed from the subclass

  • When an instance of the subclass is created, the non-private methods of the superclass are available through the subclass object.

  • Non-private methods and fields of the superclass are available in the subclass.

 

 

COSC-1437 / ITSE-2457 Computer Science Dept. - Author: Dr. Kevin Roark