Labels

.NET Job Questions About Java Absract class Abstract class Abstract Class and Interface Aggregation ajax aop apache ofbiz Apache ofbiz tutrial Association authentication autocad basics batch Binary Tree bootstrap loader in java build Builder design pattern C++ Job Questions caching CallableStatement in java certifications Chain of responsibility Design pattern charts check parentheses in a string Classes classloader in java classloading concept code quality collage level java program Composition concurrency Concurrency Tutorial Converting InputStream to String Core Java core java concept core java interview questions Core Java Interview Questions Core Java Questions core java tutorial CyclicBarrier in Java data structures database Database Job Questions datetime in c# DB Db2 SQL Replication deserialization in java Design Patterns designpatterns Downloads dtd Eclipse ejb example/sample code exception handling in core java file handling injava File I/O vs Memory-Mapped Filter first program in spring flex Garbage Collection Generics concept in java grails groovy and grails Guice Heap hibernate Hibernate Interview Questions how-to IBM DB2 IBM DB2 Tutorial ide immutable Interceptor Interface interview Interview Questions for Advanced JAVA investment bank j2ee java JAVA Code Examples Java 7 java changes java class loading JAVA Classes and Objects Java Classloader concept Java classloading concept java cloning concept java collection Java collection interview questions Java Collections java concurrency Java CountDownLatch java definiton Java design pattern Java EE 5 Java EE 6 Java Exceptions Java file Java Garbage Collection Java generics Java Glossary java hot concept java immutable concept Java Interface Java interview Question java interview question 2012 java interview question answer Java Interview Questions Java Interview Questions and Answers java interview topic java investment bank Java Job Questions java multithreading java multithreading concept java new features Java Packages java proxy object java questions Java Serialization Java serialization concept java serialization interview question java session concept java string Java Swings Questions java synchronization java threading Java Threads Questions java tutorial java util; java collections; java questions java volatile java volatile interview question Java Wrapper Classes java.java1.5 java.lang.ClassCastException JavaNotes javascript JAX-WS jdbc JDBC JDBC Database connection jdk 1.5 features JDK 1.5 new features Concurrent HashMap JMS interview question JMS tutorial job JSESSIONID concept JSESSIONID interview Question JSF jsp JSP Interview Question JSP taglib JSTL with JSP Junit Junit Concept Junit interview question.Best Practices to write JUnit test cases in Java JVM Linux - Unix tutorial Marker Interfaces MD5 encryption and decryption messaging MNC software java interview question musix NCR java interview question Networking Job Questions news Object Serialization Objects ojdbc14.jar OOP Oracle Oracle SQL Query for two timestamp difference orm own JavaScript function call in Apache ofbiz Packages Palm Apps patterns pdf persistence Portal Portlet Spring Integration Prime number test in java programs Rails Reboot remote computers REST Ruby Sample application schema SCJP security Senior java developer interviews servlet3 servlets session tracking singleton design pattern Spring Spring 2.5 Framework spring ebook Spring framework concept spring MVC spring pdf Spring Security Spring Security interview questions SQL SQL performance SQL Query to create xml file Sql Query tuning ssis and ssrs StAX and XML string concept string immutable string in java strings struts Struts2 Struts2 integration synchronization works in java Technical Interview testing tips Tomcat top Tutorial Volatile in deep Volatile working concept web Web Developer Job Questions web services weblogic Weblogic Application Server websphere what is JSESSIONID xml XML parsing in java XML with Java xslt


Monday, 22 July 2013

Inheritance Concept


Use of Inheritance

* To promote code reuse
* To use polymorphism

Inheritance in java is done by either extending a class or implementing the interfaces.

Inheritance is all about having a parent child relationship.So instead of rewriting the same functionality several times , you can just inhert those from the parent class.

Inheritance relationship can be of two types.

IS-A Relationship

Has-A Relationship

Is-A relationship is said to be direct relationship and can be done by either extending the parent class or by implementing the interface as said earlier.

For example

class Vehicle{}

class car extends vehicle{}

class ford extends car{}

In this case ford IS-A car . car IS-A Vehicle . So in the class Vehicle we can define several properties and functionalities that are similar , and declare several functionalities as abstract so that they can be overriden by the subclasses that inherit them.Since we have abstract methods in Vehicle class , we need to declare Vehicle as a abstract class.


Has-A relationship is said to be indirect relationship , in the sense , you dont want to extend or implement anything , rather create a instance of the class you want to inherit , and use that instance to invoke its methods.In this way the user dont have to know anything about the inherited class.For him,it seems like the functionality is in his Class.

for example

class Animal{}

class Halter { void tie(Rope r) { } }

class horse extends Animal {
Halter myhalter;
void tie(Rope r) { myhalter.tie(Rope r); }
}

In this case horse IS-A Animal.But Halter is not a Animal.Its just a equipment used to tie the horse. So horse Has-A Halter.Though horse doesnot extend or implement Halter it can use the 'tie' functionality by creating a instance of the Halter class.This is done as shown above.Now it seems like , we are invoking the 'tie' functionality of the horse, which in turn invokes the 'tie' functionality of Halter Class.so the actual implementation goes in Halter class and the user need not be aware of what is happening in the Halter class.For him its the tie method in the horse class is performing the operations.
While playing with minimum of 1 or 2 classes its easy to implement the concept of inheritence using concrete(normal) classes.

But in case of inheriting properties within a hierarchy or family , its better to use Abstract classes , so that you can define some functionalities that is same throughout the hierarchy and you can override several functionalities so that its more specific to that subclass.

So only the circumstance differ and not the concept of inheritence.
It is a process of hiding the implementation details from other classes and just providing a way to access and modify its data.So that ,you can make necessary changes or corrections to your code without affecting or changing the classes that inherits your class.

Consider a situation, where you write a class and your friends inherit it. your class consist of a instance variable declared public as shown below.

class your{
public int size;
}
Now, your friend assigns a value to size , that produces a error in your code.

class friends{
yours y=new yours();
yours.size=-32;
}

Though assigning -32 to size is legal, its bad for you.Because you would be expecting positive values alone. So what will you do now? you would write a setter method that check the value and assigns it to size.

So, there is a need to change your code , as well as your friends code to access the method you have implemented in your class.

This where Encapsulation comes in place.

Benefit of 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.Thus bringing in the Flexibility and Maintainability,and scalability.

In order to do that, Declare your instance variables as private. provide methods to get and set the values of the variable ,like something present in javabeans.

Now lets rewrite your class, so that it is flexible , maintainable , and scalable.

class yours{
private int size;
public int getSize() { return size; }
public void setSize(int x) { //code for checking whether x is positive size=x; }
}

Not only checking, you can also do several other operations, whithout affecting other classes that inherits it.

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...