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

Best Programming Language to learn in 2021

The main aptitude to learn in this day and age is to realize how to compose a PC program. Today, PCs have entered in pretty much every industry. Be it the autopilot in an airplane or advanced speedometer in your bicycle, PCs in different structures encompass us. PCs are incredibly valuable for an association to scale up well. Gone are the times of pen and paper. Today, to store and access your data, you totally need PCs. The programming and engineer networks are arising at a rate quicker than at any other time. Different new programming dialects are coming up that are appropriate for various classifications of designers (tenderfoots, middle, and specialists) just as for various use cases (web application, versatile applications, game turn of events, disseminated framework, and so forth) Each amateur is astounded with the inquiry, "What programming language would it be advisable for me to learn?" Types of Programming  1. Procedural Programming Languages This programming worldv

Java - Static variable, static method and static block with example.

Java Static keyword, Static method and Static block........ Questions.......?? 1) What is static keyword , static method() , static block 2) Define static keyword , static method() , static block 3) What do you mean by static keyword , static method() , static block 4)static keyword , static method() , static block in Java ?     1)Static Keyword:- Static keyword in Java Programming language is used for memory management purpose, it indicates that a particular member belong to a type itself, rather than to an instance of that type. As we mentioned above that is used for memory management purpose it means that static keyword gets memory once in the class which help in reduction of the memory, which makes the execution speed of the program fast which is a great advantage of the static keyword. let's take example how static keyword works..... firstly we will see how memory get wasted when we didn't use the static keyword. class company {  int emp_id; 

String in Java

 Topics to be covered... 1) String in Java 2)  string literal,new keyword in java 3) string array in java 4)  What is mutable string ? 5) Java StringBuffer class | StringBuffered methods() 6) reverse a string in java | how to reverse a string in java | string reverse in Java 7) string program in java --------------------------------------------------------------------------------------------------------------------------- *1)String:- string is generally a sequence of characters, but in java programming language string it is a sequence is an object which represents the sequence of the characters. -------------------------------------------------------------------------------------------------------------------------------- *2)string literal and new keyword:- string literal in java is created by using double quotes " ", let's assume an example String s=" Welcom to Programming Worlds and Facts..";   now let's see how to allocate string new keyword  String s1=&