Skip to main content

Inheritance in Java


Inheritance in Java

To know the concept of inheritance clearly you must have the idea of class and its features like methods, data members, access controls, constructors, keywords this, super etc.
As the name suggests, inheritance means to take something that is already made. It is one of the most important feature of Object Oriented Programming. It is the concept that is used for reusability purpose. Inheritance is the mechanism through which we can derived classes from other classes. The derived class is called as child class or the subclass or  we can say the extended class and the class from which we are deriving the subclass is called the base class or the parent class. To derive a class in java the keyword extends is used. To clearly understand the concept of inheritance you must go through the following example.
The concept of inheritance is used to make the things from general to more specific e.g. When we hear the word vehicle then we got an image in our mind that it moves from one place to another place it is used for traveling or carrying goods but the word vehicle does not specify whether it is two or three or four wheeler because it is a general word. But the word car makes a more specific image in mind than vehicle, that the car has four wheels . It concludes from the example that car is a specific word and vehicle is the general word. If we think technically  to this example then vehicle is the super class (or base class or parent class) and car is the subclass or child class because every car has the features of it's parent (in this case vehicle) class.
The following kinds of inheritance are there in java.
  •   Simple Inheritance
  •   Multilevel Inheritance
Pictorial Representation of Simple and Multilevel Inheritance
Simple InheritanceMultilevel Inheritance
Simple Inheritance
When a  subclass is derived simply from it's parent class then this mechanism is known as simple inheritance. In case of simple inheritance there is only a sub class and it's parent class. It is also called single inheritance or one level inheritance.     
eg.
class {
  int x;
  int y;
  int get(int p, int q){
  x=p; y=q; return(0);
  }
  void Show(){
  System.out.println(x);
  }
}

class extends A{
  public static void main(String args[]){
  A a = new A();
  a.get(5,6);
  a.Show();
  }
  void display(){
  System.out.println("B");
  }
}
Multilevel Inheritance
It is the enhancement of the concept of inheritance. When a subclass is derived from a derived class then this mechanism is known as the multilevel inheritance. The derived class is called the subclass or child class for it's parent class and this parent class works as the child class for it's just above ( parent ) class.  Multilevel inheritance can go up to any number of level.
e.g. 
class {
  int x;
  int y;
  int get(int p, int q){
  x=p; y=q; return(0);
  }
  void Show(){
  System.out.println(x);
  }
}
class extends A{
  void Showb(){
  System.out.println("B");
  }
}

class extends B{
  void display(){
  System.out.println("C");
  }
  public static void main(String args[]){
  A a = new A();
  a.get(5,6);
  a.Show();
  }
}
Java does not support multiple Inheritance
Multiple Inheritance
The mechanism of inheriting the features of more than one base class into a single class is known as multiple inheritance. Java does not support multiple inheritance but the multiple inheritance can be achieved by using the interface.
In Java Multiple Inheritance can be achieved through use of Interfaces by implementing more than one interfaces in a class.
super keyword
The super is java keyword. As the name suggest super is used to access the members of the super class.It is used for two purposes in java.
 The first use of keyword super is to access the hidden data variables of the super class hidden by the sub class.
e.g. Suppose class A is the super class that has two instance variables as  int a and float b. class B is the subclass that also contains its own data members named a and b. then we can access the super class (class A) variables a and b inside the subclass class B just by calling the following command.
super.member;
Here member can either be an instance variable or a method. This form of super most useful to handle situations where the local members of a subclass hides the members of a super class having the same name. The following example clarify all the confusions. 
class A{
  int a;
  float b;
  void Show(){
  System.out.println("b in super class:  " + b);
  }

}

class extends A{
  int a; 
  float b;
  B( int p, float q){
  a = p;
  super.b = q;
  }
  void Show(){
  super.Show();
  System.out.println("b in super class:  " super.b);
  System.out.println("a in sub class:  " + a);
  }

  public static void main(String[] args){
  B subobj = new B(15);
  subobj.Show();
  }
}
Output:
C:\>java B
b in super class: 5.0
b in super class: 5.0
a in sub class: 1
Use of super to call super class constructor: The second use of the keyword super in java is to call super class constructor in the subclass. This functionality can be achieved just by using the following command.
super(param-list);
Here parameter list is the list of the parameter requires by the constructor in the super class. super must be the first statement executed inside a super class constructor. If we want to call the default constructor then we pass the empty parameter list. The following program illustrates the use of the super keyword to call a super class constructor. 
class A{
  int a;
  int b;
  int c;
  A(int p, int q, int r){
  a=p;
  b=q;
  c=r;
  }
}
  
  class extends A{
  int d;
  B(int l, int m, int n, int o){
  super(l,m,n);
  d=o;
  }
  void Show(){
  System.out.println("a = " + a);
  System.out.println("b = " + b);
  System.out.println("c = " + c);
  System.out.println("d = " + d);
  }

  public static void main(String args[]){
  B b = new B(4,3,8,7);
  b.Show();
  }
  }
Output:
C:\>java B
a = 4
b = 3
c = 8
d = 7

Comments

Popular posts from this blog

Hi! I'm Java...

Java is a computer programming language. It enables programmers to write computer instructions using English based commands, instead of having to write in numeric codes. It’s known as a “high-level” language because it can be read and written easily by humans. Like English, Java has a set of rules that determine how the instructions are written. These rules are known as its “syntax”. Once a program has been written, the high-level instructions are translated into numeric codes that computers can understand and execute. Who Created Java? In the early nineties, Java was created by a team led by James Gosling for Sun Microsystems. It was originally designed for use on digital mobile devices, such as cell phones. However, when Java 1.0 was released to the public in 1996, its main focus had shifted to use on the Internet. It provided more interactivity with users by giving developers a way to produce animated webpages . Over the years it has evolved as a successful language for

I'm the Server Operating System

Server Operating System A server operating system is software that was especially developed to serve as a platform for running multi-user computer programs, applications that are networked and programs critical to business computing. This type of operating system (OS) often comes bundled with the most common types of applications deployed in the client-server model, a term used to indicate the exchange of information between computers. For example, a hypertext transfer protocol (HTTP) or web server hosts or "holds" the text files, images files and scripts that work together to form a website. When someone connects to the Internet and types in a web address, the server that holds the files of the site delivers or "serves" the requested pages to the client computer or the machine that made the request. Frequently used applications in the client-server model handle operations for sharing files and printers across a network, hosting and serving web pages via the

A Hit Count Using Java Servlet's Session Tracking

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class SessionTracker extends HttpServlet {         public void doGet(HttpServletRequest req,HttpServletResponse res)         throws ServletException,IOException         {                res.setContentType("text/html");                PrintWriter out=res.getWriter();                HttpSession session=req.getSession(true);                Integer count=(Integer)session.getValue("tracker.count");                if(count==null)                {                         count=new Integer(1);                }else{                         count=new Integer(count.intValue()+1);                }                session.putValue("tracker.count",count);                out.println("<HTML><BODY><H1>SESSION TRACKING DEMO</H1>");                out.println("You've visited this page "+count+((count.intValue()==1)?" time.":"times."