Which keyword would be used in a subclass to call the constructor in a parent class?

Core Concepts

Inheritance has the same core concepts in any object-oriented language
  • One class is derived from another, inherits all its data and methods
  • Terminology
    • superclass, base class, parent class
    • subclass, derived class, child class. Also called an extended class in Java

Inheritance in Java

Use the keyword extends to declare the derived class
  // Example 1
  public class AAA		// AAA is the base class
  { ... }

  public class BBB extends AAA  // BBB is the derived class
  { ... }


  // Example 2
  public class Employee {...}				// base class
  public class HourlyEmployee extends Employee { ... }  // derived

keyword super

You can use super like a function call in a derived class constructor -- invokes the base class constructor
  super();		// invokes base class default constructor
  super(parameters);    // invokes base class constructor with parameters

  // Example, for a class called HourlyEmployee, derived from Employee
  public class HourlyEmployee extends Employee
  {
    public HourlyEmployee()	// default constructor
    {
	super();		// invokes Employee() constructor
    }

    public HourlyEmployee(double h, double r)
    {
	super(h,r);		// invokes Employee constructor w/ 2 parameters
    }
    
    // ... more methods and data

  } // end class HourlyEmployee
  • The call to super() must be the first line of the derived class constructor
  • If explicit call to parent constructor not made, the subclass' constructor will automatically invoke super(). (the default constructor of the base class, if there is one)
  • Can also use super to invoke a method from the parent class (from inside the derived class). Format:
      super.method(parameters)
    

The protected modifier

  • Like in C++, protected data and methods of a public class can be accessed by any classes derived from the given class.
  • In Java, a protected member can also be accessed by any class in the same package

The final modifier

Use in Java for a few special inheritence-related purposes:
  • When used on a class declaration, it means that the class cannot be extended. (i.e. it cannot become a parent class to a new subclass)
  • When used on a method declaration, it means that the method cannot be overridden in a subclass. (i.e. this is the final version of the method)

Other differences

  • Java does not have multiple inheritance. A class can only extend one other class
    • C++ has multiple inheritance (a class can be derived from more than one base class
  • Java has a construction known as an interface. This is actually a keyword in Java. It allows behavior similar to inheritance, especially regarding polymorphism
  • In Java, every class is part of an inheritance hierarchy, because the class called Object (java.lang.Object) is automatically a base class to every Java class

Method Overriding

Just like in C++, this is when a derived class has a method with the same prototype as a method in the base class. (The derived class function overrides the base class version, when called for a derived object).

Example:

  • Suppose a class Rectangle is derived from class Shape.
  • Shape has a method:
      void Draw() { ... }  
    
  • We can define a method in class Rectangle with the same signature. The derived class version will override the base class version, when called through an object of type Rectangle.
      Rectangle r = new Rectangle();  // create a Rectangle object
    				  //  which has all the Shape methods available
    
      r.Draw();			  // invokes the Draw method from the
    				  //  Rectangle class
    
  • Note that the Rectangle class' Draw() method can still invoke the superclass' method, with the keyword super
      public void Draw()
      {
        super.Draw();		// invoke parent's Draw()
    
        // continue with any processing specific to Rectangle
      }
    

Abstract Classes

  • Like in C++, an abstract class is one that cannot be instantiated
  • To make a class abstract in Java, use the keyword abstract (which is a modifier)
      public abstract class Shape
    
  • Now that Shape is abstract, this would be illegal:
      Shape s = new Shape();	// specifically, it's  new Shape(); 
    				//   that is illegal
    
Java methods can be abstract as well:
  • An abstract method is a method signature without a definition
  • Abstract methods can only be created inside abstract classes
  • The main purpose of an abstract method is to be overridden in some derived class, or classes
  • Example:
      public abstract class Shape		// Shape is an abstract class
      {
        public abstract double findArea();	// findArea is an abstract method
    
        // other methods and data
      }
    
  • Note that a Java abstract method is like a pure virtual function in C++
  • In C++, a class is abstract if it has one or more pure virtual member functions. In Java, you just use the keyword abstract when declaring it

Which keyword can be used to call the constructor of a parent class?

3) super is used to invoke parent class constructor. The super keyword can also be used to invoke the parent class constructor.

Can a subclass call the parent's class constructor and when?

The parent constructor must be called by the subclass's constructor, however this does not always require an explicit super() call. If the parent class has a default (no-argument) constructor, then it will automatically be called by the subclass constructor if you omit the call to super() .