Skip to main content

Inheritance In Java

 Inheritance

Inheritance is a process or a mechanism where one class acquire the properties which include methods and fields of another class. With the use of this concept the information is made manageable in a hierarchical order.

The class which inherits the properties of another class is known as subclass or derived class or child class. and the class whose properties are inherited is called as base class or parent class.

In java inheritance cannot be achieved until we use extends keyword.

Reason to use Inheritance

1 For Method overriding

2 For code reusability 

Syntax:

class abc {

.........

.........

}

class xyz extends abc {

.........

.........

}

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

Example

//WAP to Demonstrate Inheritance.............

import java.io.*;

class Superclass

{

BufferedReader r=new BufferedReader(new InputStreamReader(System.in));

Double num1,num2;

void get()

{

try

{

     System.out.print("Enter the 1st number:- ");

            num1=Double.parseDouble(r.readLine());

            System.out.print("Enter the 2nd number:- ");

            num2=Double.parseDouble(r.readLine());

}

catch(Exception ex)

{

}

}

void put()

{

System.out.println ("1st number:- "+num1);

System.out.println ("2nd number:- "+num2);

}

}

class Subclass extends Superclass

{

Double num3;

void display()

{

num3=num1*num2;

System.out.println ("Multiplication = "+num3);

}

}

class single2

{

public static void main (String[] args) {

Subclass s1= new Subclass();

s1.get();

s1.put();

s1.display();

}

}

Output

Enter the 1st number:- 12

Enter the 2nd number:- 2

1st number:- 12.0

2nd number:- 2.0

Multiplication = 24.0

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

Types of Inheritance

1 Single inheritance

2 Multilevel inheritance

3 Hierarchical inheritance

Single inheritance

In single inheritance a single class is inherited from the base class.

Multilevel inheritance

In multilevel inheritance a class is derived from a class which is derived from the base class

Hierarchical inheritance

when more than one class is inherited from the base class is called as hierarchical inheritance.


Comments

Post a Comment

Popular posts from this blog

Decision Making Statements in Java Programming Language.

 Their are 3 decision making statement in Java. Here where ever you find the if underlined like this( if ) then it is related to statement. 1) if statement. 2) if else statement. 3) if-else-if statement. So lets discuss one by one about each statement. 1) if statement:- if statement is used in programming for checking the given condition is true or not. If the condition in the if block is true then the block executes otherwise the programme terminates. Lets understand it by its syntax.   Here in the syntax we have condition in the if block, if the the condition which we have putted is true then the code which is written in the block will executes otherwise the programme ends their.  2) if else statement:- if else statement is used in programming for checking the given condition is true or false. If the condition is true then the if block is executed otherwise the else block is executed. Lets understand it by its syntax. Here in the syntax a condition is given in the if...

What is Procedural Programming Language(PPL)

  Procedural Programming Language. Procedural Programming might be the primary programming worldview that another engineer will learn. Essentially, the procedural code is the one that straightforwardly trains a gadget on the best way to complete an errand in consistent advances. This worldview utilizes a direct top-down methodology and treats information and techniques as two distinct elements. In view of the idea of a methodology call, Procedural Programming isolates the program into strategies, which are otherwise called schedules or capacities, just containing a progression of steps to be done.  Basically, Procedural Programming includes recording a rundown of directions to mention to the PC what it ought to do bit by bit to complete the job needing to be done.  Key Highlights of Procedural Programming  The vital highlights of procedural writing computer programs are given underneath:  1)Predefined capacities: A predefined work is normally a guidance distingu...

Packages In Java

  Package in java is a group of similar type of classes, interface and sub-package. Packages in java can be defined in two ways which are 1 Built-in Packages 2 User-defined Packages Built-in Packages are       * Lang      * Util      * io      * awt      * net      * applet, etc. User-defined packages can be anything which are created by the user. Packages are nothing but container of the classes which store the no of classes, interface and their methods. Advantages of Packages 1 It provide access Protection 2 It removes the naming collision 3 it is used to categorize the classes and interface so that they can be easily maintained. Steps for creating a package Step-1 Create a folder in any drive of your choice  Step-2 example in d: package1 (it is the name of the package) Step-3 Open the java application and type the following code package package1; public class demo {  publ...