Challenge

Project 1

4a. output: b c a 100
explanation: The program had a public class names as 'StaticBlock' which initially prints the variable output as "b" of static, which is an instance of a class.
Then after the main method of this program has to print the output as "c" as in the ordered flow.
And after, the final output is in the main method, which is calling the Scalar.i which prints the output as "a", because in Scalar, the variable output is printed first
after that the i value is initialized as 100, so it will be printed next accordingly.

4b. output: class A class B class C
explanation: The code is trying to create a class called C. The code creates an object of type C and prints the string "Class C" on the console.
The main method is defined in Sol_4b, which is public static void main(String[ ] args).
This means that it can be accessed by any other class or program.
It takes one argument: String[ ] args, which are arguments passed into the program when it was executed from a command line interface such as Terminal or Command Prompt.
The code is an example of a class that extends another class.
The super keyword will be used to refer to the parent class in this case, which would be A.

5a. Difference between method overloading and method overriding in java:
method overloading:
*Method overloading is a compile-time polymorphism.
*It helps to increase the readability of the program.
*It gives better performance. The reason behind this is that the binding of overridden methods is being done at runtime.
*Method overloading may or may not require inheritance.
*It occurs within the class.
method overriding:
*Method overriding is a run-time polymorphism.
*It is used to grant the specific implementation of the method which is already provided by its parent class or superclass.
*Poor performance.
*Method overriding always needs inheritance.
*It is performed in two classes with inheritance relationships.

5b. On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.
In java programming, multiple and hybrid inheritance is supported through interface only.

5c. difference between Composition and Inheritance:
Composition:
* In composition we only define a type which we want to use and which can hold its different implementation also it can change at runtime. Hence, Composition is much more flexible than Inheritance.
*composition allows to use functionality from different class.
*Composition allows to test the implementation of the classes we are using independent of parent or child class.
*composition allows code reuse even from final classes.
*While it is a has-a relationship.
Inheritance:
*In inheritance, we define the class which we are inheriting(super class) and most importantly it cannot be changed at runtime.
*Here we can only extend one class, in other words more than one class can’t be extended as java do not support multiple inheritance.
*In inheritance we need parent class in order to test child class.
*Inheritance cannot extend final class.
*It is an is-a relationship.

5d. No, we cannot override private or static methods in Java.
Private methods in Java are not visible to any other class which limits their scope to the class in which they are declared.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.