Encapsulation


The ability to make changes in your implementation code without breaking the
code of others who use your code is a key benefit of encapsulation. You want to
hide implementation details behind a public programming interface. By interface,
we mean the set of accessible methods your code makes available for other code to
call—in other words, your code's API. By hiding implementation details, you can
rework your method code without forcing a change in the code that calls your changed method


Inheritance, Is-A, Has-A


every class in Java is

a subclass of class Object,

In other words, every class you'll ever use or ever write will inherit from class Object. You'll always have an equals method, a clone method, notify, wait, and others, available to use. Whenever you create a class, you automatically inherit all of class Object's methods



Two most


common reasons to use inheritance are


■ To promote code reuse
■ To use polymorphism


IS-A

In OO, the concept of IS-A is based on class inheritance or interface implementation. IS-A is a way of saying, "this thing is a type of that thing.


You express the IS-A relationship in Java through the keywords extends (for class inheritance) and implements (for interface implementation).

eg:


public class Vehicle { ... }
public class Car extends Vehicle { ... }
public class Subaru extends Car { ... }


HAS-A

HAS-A relationships are based on usage, rather than inheritance. In other words,
class A HAS-A B if code in class A has a reference to an instance of class B

eg:


public class Animal { }
public class Horse extends Animal {
private Halter myHalter;
}

---------------


public class Horse extends Animal {
private Halter myHalter = new Halter();
public void tie(LeadRope rope) {
myHalter.tie(rope); // Delegate tie behavior to the
// Halter object
}
}
public class Halter {
public void tie(LeadRope aRope) {
// Do the actual tie work here
}
}

Leave a Reply

Subscribe to Posts | Subscribe to Comments

About This Site

Howdy! My name is Suersh Rohan and I am the developer and maintainer of this blog. It mainly consists of my thoughts and opinions on the technologies I learn,use and develop with.

Blog Archive

Powered by Blogger.

- Copyright © My Code Snapshots -Metrominimalist- Powered by Blogger - Designed by Suresh Rohan -