Answer to Question #287911 in Java | JSP | JSF for Zagar

Question #287911

Answer the following.

1. By consulting any good resource, find out what inner classes are.

2. Briefly explain how method overriding is different from method overloading.

3. Use the brief code example to illustrate the situation where an abstract class may prefer to use rather than a concrete class.

4. Explain the difference between how method parameters are passed for variables that contain object references and variables that contain primitive data types.


1
Expert's answer
2022-01-17T08:25:19-0500
  1. Java inner class or nested class is a class that is declared inside the class or interface.We use inner classes to logically group classes and interfaces in one place to be more readable and maintainable.Additionally, it can access all the members of the outer class, including private data members and methods.
  2. Method Overloading is a Compile time polymorphism. In method overloading, more than one method shares the same method name with a different signature in the class. In method overloading, the return type can or can not be the same, but we have to change the parameter because, in java, we can not achieve the method overloading by changing only the return type of the method. At the same time method Overriding is a Run time polymorphism. In method overriding, the derived class provides the specific implementation of the method that is already provided by the base class or parent class. In method overriding, the return type must be the same or co-variant.
abstract class Shape {
    abstract void draw();
}

//In real scenario, implementation is provided by others i.e. unknown by end user  
class Rectangle extends Shape {
    void draw() {
        System.out.println("drawing rectangle");
    }
}

class Circle1 extends Shape {
    void draw() {
        System.out.println("drawing circle");
    }
}

//In real scenario, method is called by programmer or user  
class TestAbstraction1 {
    public static void main(String args[]) {
        Shape s = new Circle1();//In a real scenario, object is provided through method, e.g., getShape() method  
        s.draw();
    }
}

4.. When you pass a primitive, you actually pass a copy of value of that variable. This means changes

done in the called method will not reflect in original variable. When you pass an object you don't

pass a copy, you pass a copy of 'handle' of that object by which you can access it and can change

it.


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog