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.
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.
Comments
Leave a comment