Overloading Overriding Runtime Type and Object Orientation

19) State the benefits of encapsulation in object oriented design and write code that implements tightly encapsulated classes and the relationships "is a" and "has a".


"is a" versus "has a"

Everything is an object. For example, buildings, furniture, animals etc. A class is made up of objects. Any object belonging to this class will share these characteristics and behaviors.
Think of an object as a unit that can be made up of other smaller units. Think of an apartment building, for example. It is made up of number of apartments. Each apartment has doors, windows, a kitchen, a bedroom, and a bathroom. Apartments are the objects in this example. The apartment building is the class. It is made up of objects, or units. These objects are not all exactly alike, but they have enough similar characteristics that they can be classed together.

To distinguish between "is a" and "has a", take the above example.
Now, how would you decide whether to use inheritance or nesting to associate the two.
Would you say "apartment has a door" or "apartment is a door"?
Your answer should be "has a", so use nesting.

Would you say "apartment is a building" or "apartment has a building"?
Your answer should be "is a", so use inheritance to extend the building class in the apartment class.

Encapsulation :

One of the important object-oriented techniques is "encapsulation". The technique of hiding the data within the class, and making it available only through the methods is known as encapsulation, because it seals the class's data safely inside the "capsule" of the class, where it can be accessed only by trusted users--i.e., by the methods of the class.

eg,
class building {
private int no_of_apts=30;
protected int bedroom;
protected int bathroom;
public void num_of_doors(int door) {
int num = door;
System.out.println("Total number of doors : " + num);
}
public int setMethod() {
return no_of_apts;
}
}

class apartment extends building {
int apt_number;
int resident_name;
public void num_of_doors(int door) {
int num = door;
System.out.println("Number of doors in an apt : " + num);
}

}

public class inherit_test {
public static void main(String arg[]) {
building b = new building();
apartment a = new apartment();
System.out.println(b.no_of_apts); //compile error :
//Variable no_of_apts in class building not accessible from class

//inherit_test
int apts=b.setMethod(); //data is accessed thro' a method and not directly.
//It is encapsulated.

System.out.println("Number of Apartments : " + apts);
b.num_of_doors(15);
a.num_of_doors(2);
}
}

Remember :




20) Write code to invoke overridden or overloaded methods and parental or overloaded constructors; and describe the effect of invoking these methods.

A method is a name for an action. You refer to all objects and methods by using names. Well-chosen names make it easier for you and others to understand your code.

Overloading :

Say, there is a class called Drink and a subclass called hotDrink. For our example we will take coffee and tea. Both has a preparation method, but the ingredients and outcome are different.

Overloading is almost a must for constructors. Say, you want to create an object in more than one way, ie, you need two constructors, one that takes no argument, and one that takes an int argument, and there can be only one constructor name as constructor's name is predetermined by the name of the class, you need method overloading.

eg,
class A {
int a;
A() {
a=0;
}
A(int i) {
a=i;
System.out.println(a);
}
public void method() {
}//no parameters, return type is void

public void method(String[] c) {
}//one parameter, return type is void

public int method(int a, int b) {
}//two parameters, return type is int
}
public class overload {
public static void main(String[] arg) {
A aClass = new A();
A bClass = new A(5);
aClass.method();
bClass.method("hi");
aClass.method(5,6);
}
}

eg using this keyword,
class A {
public A(String a) {
this(int i, int j);
}
public A( int m, int n) {
int k=m;
int l=n;
}
........
}

when you call the constructor new A("hi"), then the A(String) constructor calls the A(int, int) constructor.
This will be useful when there are common code between constructors.

As overloaded methods take a unique list of argument types, Java can easily find which method to call.

Overriding :

Say, there is a class called hotDrink and a subclass called tea. tea can be cold or hot. Though you can use the same ingrediants the preparation differs. So you have to implement new versions of these functions.

eg see also the use of the keyword super,
class overridden {
int i = 5;
public void method() {
System.out.println(i);
}
}

class override extends overridden {
int i=10;
public void method() {
System.out.println("i in override : " + i);
i = super.i;
System.out.println("i in overridden : " + i);
}
public static void main(String s[]) {
override o = new override();
o.method();
}
}

output :
i in override : 10
i in overridden : 5

Overloaded
Overridden
supplements each other (largely) replaces the method it overrides
can exist in any number within same class Each method in a parent class can be overridden atmost once in any one class
must have different arguments must have argument list of identical type and order
return type may be freely chosen return type must be identical
determined at compile time determined at runtime

Remember :



21) Write code to construct instances of any concrete class including normal top level classes inner classes static inner classes and anonymous inner classes.

You can create four different types of inner classes, based upon the how and the where of creation.