Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Let\'s start off by describing how Java specifies that one class inherits from a

ID: 665252 • Letter: L

Question

Let's start off by describing how Java specifies that one class inherits from another class. Are there any restrictions on inheritance in Java that distinguish it from C++? How does a derived class override a base class method? How does an overridden method invoke a base class method? How does the derived class constructor invoke an explicit value constructor from the base class?

What do you guys think of pseudocode? There are MANY variants of how to write pseudocode (there aren't any hard and fast rules) so are there any variants you've found useful?

Explanation / Answer

--------------------------------------------------------------------------------------
Q1: Are there any restrictions on inheritance in Java that distinguish it from C++?
--------------------------------------------------------------------------------------

   Inheritance: Acquiring properties from base class to derived class is called inheritance.

   Although inheritance concept is same in both C++ and JAVA.But There are some differences between C++ and JAVA.

   ---------------------------------------------------------------------
   The following are the differences in C++ And JAVA inheritance
   ---------------------------------------------------------------------

   1.    In java all classes inherits from Object class directly or indirectly.
   Hence, there is always a single tree of inheritance classs in java,with root of the tree is Object class.
   But where in C++ It is forest of classes, Any class we create does not inherit from anything.

   2.   Protected key word differnce, In JAVA protected members of a class can be accessed in
   inherited classed as well as the classes in the same package can access the members.
   But where in C++ only inherited class can access the protected member.

   3.   JAVA uses extends keyword for inheritence, But where C++ can use access specifier
   public or private or protected.

   4.   JAVA uses interface keyword for interfaces. and abstract keyword for abtract classes.

   5.    In Both JAVA and C++ default constructors for base classes automatically called.
   When coming to paramiterized constructors.
   JAVA uses super keyword to call its parent class constructor.
   Where as in C++ it needs to call using the parent class construtor only.

--------------------------------------------------------------------------------------
Q2: How does a derived class override a base class method?
--------------------------------------------------------------------------------------

   A sub class has same method in parent is callled overriding.
   -----------------
   Example Program
   -----------------

   class Vehicle {
  
       public void start() {
           System.out.println("Vehicle is started");
       }
   }

   public class Bus extends Vehicle{
  
       public void start() {
           System.out.println("Bus is started");
       }
  
       public static void main(String[] args) {
           Bus bus = new Bus();
           bus.start();
       }

   }
   -------------
   Outout
   -------------

   Bus is started
--------------------------------------------------------------------------------------
Q3: How does an overridden method invoke a base class method?
--------------------------------------------------------------------------------------

   An overridden method can invoke th base class method using super keyword.

   ------------------
   Example program
   ------------------

   class Vehicle {
  
       public void start() {
           System.out.println("Vehicle is started");
       }
   }

   public class Bus extends Vehicle{
  
       public void start() {
           super.start();
       }
  
       public static void main(String[] args) {
           Bus bus = new Bus();
           bus.start();
       }

   }


   -------------
   Outout
   -------------

   Vehicle is started

--------------------------------------------------------------------------------------
Q4: How does the derived class constructor invoke an explicit value constructor from the
base class?
--------------------------------------------------------------------------------------

   A derived class consructor can invoke an parent class constructor using super keyword.

   ------------------
   Example program
   ------------------

   class Vehicle {
  
       private String vehicleType;
  
       public Vehicle(String vehicleType) {
           this.vehicleType = vehicleType;
           System.out.println("Vehicle constructor is called and type is "+vehicleType);
       }
  
       public void start() {
           System.out.println("Vehicle is started");
       }
   }

   public class Bus extends Vehicle{
  
       public Bus(String vehicleType) {
           super(vehicleType);
       }
  
       public void start() {
           super.start();
       }
  
       public static void main(String[] args) {
           Bus bus = new Bus("Four Wheeler");
           bus.start();
       }

   }


   -------------
   Outout
   -------------

   Vehicle constructor is called and type is Four Wheeler
   Vehicle is started
--------------------------------------------------------------------------------------
Q5: What do you guys think of pseudocode? There are MANY variants of how to write pseudocode (there aren't any hard and fast rules) so are there any variants you've found useful?
--------------------------------------------------------------------------------------

   Pseudocode is a simple way of undestanding any program. Which gives overview of a program using simple steps.
The rules of Pseudocode all statements include while, do, for, if, switch. Examples below will illustrate this notion.
Flowchart is the best way to understand a program.