Tuesday 7, September 2010
      Home       Tutorials       Free Ebooks       Free Scripts       Tech News       Articles       About Us

Inheritance in Java

Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An example of where this could be useful is with an employee records system. You could create a generic employee class with states and actions that are common to all employees. Then more specific classes could be defined for salaried, commissioned and hourly employees. The generic class is known as the parent (or superclass or base class) and the specific classes as children (or subclasses or derived classes). The concept of inheritance greatly enhances the ability to reuse code as well as making design a much simpler and cleaner process.

The Object Class
The Object class is the highest superclass (ie. root class) of Java. All other classes are subclasses (children or descendants) of the Object class. The Object class includes methods such as:


clone()

equals()

copy(Object src)

finalize()

getClass()

hashCode()

notify()

notifyAll()

toString()

wait()

Inheritance in Java
Java uses the extends keyword to set the relationship between a parent class and a child class.

public class GraphicsBox extends Box

The GraphicsBox class assumes or inherits all the properties of the Box class and can now add its own properties and methods as well as override existing methods. Overriding means creating a new set of method statements for the same method signature (name, number of parameters and parameter types). For example:

// define position locations
    private int left, top;
// override a superclass method
    public int displayVolume() {
      System.out.println(length*width*height);
      System.out.println("Location: "+left+", "+top);
      }

When extending a class constructor you can reuse the superclass constructor and overridden superclass methods by using the reserved word super. Note that this reference must come first in the subclass constructor. The reserved word this is used to distinguish between the object's property and the passed in parameter.

    GraphicsBox(l,w,h,left,top)
    {
      super (l,w,h);
      this.left=left;
      this.top=top;
    }
    public void showObj()
    {System.out.println(super.showObj()+"more stuff here");}

The reserved word this can also be used to reference private constructors which are useful in initializing properties.
Special Note:You cannot override final methods, methods in final classes, private methods or static methods.
Abstract Class
As seen from the previous example, the superclass is more general than its subclass(es). The superclass contains elements and properties common to all of the subclasses. The previous example was of a concrete superclass that instance objects can be created from. Often, the superclass will be set up as an abstract class which does not allow objects of its prototype to be created. In this case, only objects of the subclass are used. To do this the reserved word abstract is included in the class definition.
Abstract methods are methods with no body specification. Subclasses must provide the method statements for their particular meaning. If the method was one provided by the superclass, it would require overriding in each subclass. And if one forgot to override, the applied method statements may be inappropriate.
public abstract class Animal  // class is abstract
{
  private String name;
  public Animal(String nm)      // constructor method
  { name=nm; }
  public String getName()       // regular method
  { return (name); }
  public abstract void speak(); // abstract method - note no {}
}
Abstract classes and methods force prototype standards to be followed (ie. they provide templates).

   
 
Java Language Tutorial
 
  What is JAVA
What is JAVA
  Java an Object Oriented Language
Java an Object Oriented Language
  Java Keywords
Java Keywords
  Data Types
Data Types
  Java Operators
Java Operators
  Control Structure
Control Structure
  Arrays
Arrays
  Java Classes
Java Classes
  Java Constructors
Java Constructors
  Java Methods
Java Methods
  Java Method Overloading
Java Method Overloading
  Java Method Overriding
Java Method Overriding
  Java Nested Classes
Java Nested Classes
  Inheritance in Java
Inheritance in Java
  Interfaces in Java
Interfaces in Java
  Polymorphism in Java
Polymorphism in Java
  Exception Handling in Java
Exception Handling in Java
  Catching Exceptions
Catching Exceptions
  Embedding information in an exception object
Embedding information in an exception object
  The throws clause
The throws clause
  Checked and unchecked exceptions
Checked and unchecked exceptions
  The finally clause
The finally clause
  Applets in Java
Applets in Java
  Running an Applet
Running an Applet
 
 
 
Web Designing Tutorials
  HTML Tutorial
HTML Tutorial
  DHTML Tutorial
DHTML  Tutorial
  CSS Tutorial
CSS Tutorial
  XHTML Tutorial
XHTML Tutorial
 
Programming Languages Tutorials
  C Language Tutorial
C Language Tutorial
  C++ Tutorial
C++ Tutorial
  Java Language Tutorial
Java Language Tutorial
  Data Structure Theory Tutorial
Data Structure Theory Tutorial
 
Server Side Scripting Tutorials
  PHP Tutorial
PHP Tutorial
  SQL Tutorial
SQL Tutorial
 
Client Side Scripting Tutorials
  JavaScript Tutorial
JavaScript Tutorial
  VBScript Tutorial
VBScript Tutorial
 
 
 
POPULAR E-BOOKS
 
Download How to Receive Goods FOR FREE Ebook How to Receive Goods FOR FREE
   
Download 30 Days To Create A Product Ebook 30 Days To Create A Product
   
Download Its An Internet Jungle Out There Ebook Its An Internet Jungle Out There
   
Download Creating Wealth Boot Camp Ebook Creating Wealth Boot Camp
   
Download 30 days to Create a product Ebook 30 days to Create a product
   
Technology News      
Studiesinn.com © 2010 All Rights Reserved.