answersLogoWhite

0


Best Answer

By using Inheritance. By using Inheritance once class can re-use or inherit the features of another class.

Ex:

public class Ferrari extends Car {

...

}

Here, the class Ferrari reuses code/features of the class Car.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

Reusability is implemented in java with the help of inheritance.

the two most common reasons to use inheritance are

• To promote code reuse (You don't want your colleague sitting right next to you to write the same validation code in his class while he can obviously use the better code you have written :-) )

• To use polymorphism

Let's begin with reuse.

A Class can re-use or in java terms inherit features/code of another class by just extending it.

Ex:

public class Child extends Parent {

}

In the above example by virtue of the extends keyword, all the public code inside the parent class is available for use inside the child class.

A common design approach is to create a fairly generic version of a class with the intention of creating more specialized subclasses that inherit from it. For example:

class Car {

public void goForADrive() {

System.out.println("driving a car");

}

// more code

}

class Ferrari extends Car {

public void takeFerrariForADrive() {

System.out.println("driving a Ferrari");

}

// more code

}

public class TestCars {

public static void main (String[] args) {

Ferrari car = new Ferrari();

car.goForADrive();

car.takeFerrariForADrive();

}

}

Outputs:

driving a car

driving a Ferrari

Notice that the Ferrari class inherits the generic goForADrive() method from the less-specialized class Car, and also adds its own method, takeFerrariForADrive(). Code reuse through inheritance means that methods with generic functionality that could apply to a wide range of different kinds of cars -don't have to be reimplemented. That means all specialized subclasses of Car are guaranteed to have the capabilities of the more generic superclass. You don't want to have to rewrite the goForADrive() code in each of your specialized car classes of a racing game.

But you probably already knew that. You've experienced the pain of duplicate code when you make a change in one place and have to track down all the other places where that same piece code exists. So it is logically and obviously easier to manage changes if all common code is available at one single place.

The second use of inheritance is to allow your classes to be accessed polymorphically-a capability provided by interfaces as well, but we'll get to that in a minute. Let's say that you have a CarLauncher class that wants to loop through a list of different kinds of Car objects, and invoke goForADrive() on each of them. At the time you write this class, you don't know every possible kind of Car subclass that anyone else will ever write. And you sure don't want to have to redo your code just because somebody decided to build a new Car six months later.

The beautiful thing about polymorphism is that you can treat any subclass of Car as a Car. In other words, you can write code in your CarLauncher class that says, "I don't care what kind of object you are as long as you inherit from (extend) Car. And as far as I'm concerned, if you extend Car then you've definitely got a goForADrive() method, so I know I can call it."

Imagine we now have two specialized subclasses that extend the more generic Car class, Ferrari and Porsche:

class Car {

public void goForADrive() {

System.out.println("driving a car");

}

// more code

}

class Ferrari extends Car {

public void takeFerrariForADrive() {

System.out.println("driving a Ferrari");

}

// more code

}

class Porsche extends Car {

public void takePorscheForADrive() {

System.out.println("driving a Porsche");

}

// more code

}

Now imagine a test class has a method with a declared argument type of Car, that means it can take any kind of Car. In other words, any subclass of Car can be passed to a method with an argument of type Car. This code

public class TestCars {

public static void main (String[] args) {

Ferrari ferrari = new Ferrari();

Porsche Porsche = new Porsche();

goForADrive(ferrari);

goForADrive(Porsche);

}

public static void doDrive(Car car) {

car.goForADrive();

}

}

Outputs:

driving a car

driving a car

The key point is that the doDrive() method is declared with a Car argument but can be passed any subtype (in this example, a subclass) of Car. The method can then invoke any method of Car, without any concern for the actual runtime class type of the object passed to the method. There are implications, though. The doDrive() method knows only that the objects are a type of Car, since that's how the parameter is declared. And using a reference variable declared as type Car-regardless of whether the variable is a method parameter, local variable, or instance variable-means that only the methods of Car can be invoked on it. The methods you can call on a reference are totally dependent on the declared type of the variable, no matter what the actual object is, that the reference is referring to. That means you can't use a Car variable to call, say, the takePorscheForADrive() method even if the object passed in is of type Porsche.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Reusability in Java means that once you write some code, you can use it over and over again - if you write it so that it can be reused. For example, if you write a Enumeration-Iterator adapter, or some code that lets you treat an Enumeration - Java legacy code - as an Iterator - shiny NEW Java code - then you can use it not only in your animal simulation program, but also in your Jacuzzi interface program.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

You identify where you are repeating code (or where you might repeat it, assuming you didn't write it already). Then, you have several options.A "for" or "while" loop can repeat actions within one method. You need to identify what changes from one repetition to the next, and use some variable or variables for that.

Another option is to put the repeated code into a separate method, or even a separate class.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you avoid repeating code in Java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you do this java code?

You have to specify the task that you wish me to help you with the Java code.


What does the Java compiler translate Java source code to?

The Java compiler translates Java source code to Java byte code.


What is a simple java code and explain what the code does?

int a;This simple Java statement declares an integer.


In the first step of the Java hybrid compilation process Java program code is converted into an intermediate format called .?

Java byte code.


What kind of file contain java source code?

'.java' files contain java source code. One can access these files on windows by using 'notepad'.


Difference between a java complier and the Java JIT?

A java compiler takes Java source code and turns it into Java bytecode, which can then be run by the java virtual machine.Using JIT means that the java code will be compiled and executed at the time that you run the program, which will slow down the program because it has to compile the code at the same time that it runs.


How can add java code to java server page?

Scriplet.


What are the technologies without using java?

you can use type writer to avoid using java :)


Why you prefer to having parent class in java?

Having a parent class in Java is not mandatory. It is a good approach to extend from a parent class that has much of the functionality that the current class needs already coded. This helps us re-use functionality/code and avoid redundant/duplicate code.


Java code for tax deduction in database connection?

Java code for tax deduction in database connection?


How is java byte code is different from other low level language?

Java byte-code is the code which generate after the compilation of .java file.And this code is only understand by JVM(java virtual machine ) which understand it and execute it.In other languages this type of functionality is not available.


What is Java byte-code level language?

Java byte-code is the code which generate after the compilation of .java file.And this code is only understand by JVM(java virtual machine ) which understand it and execute it.In other languages this type of functionality is not available.