Advertisement

Explain inheritance (in java)

Inheritance

        Inheritance is one of the key features of object oriented programming. Inheritance provided mechanisms that allowed a class to inherit property of another class. When a class extends another class it inherits all non-private members including fields and methods. 
Inheritance in java can be best understand in term of parent and child relationship, also known as super class(parent) and sub class(child) in java language.


Inheritance defines is-a relationship between a super class and its sub-class extends and implements keywords are used to describe inheritance in java.


purpose of inheritance

  1.         It promotes the code  reusability i.e . the same methods are variables which are defined in a parent /super/base class can be used in the child/drived class.
  2. It promotes polymorphism by allowing method overriding.      
                       syntax: - class < drive class name> extends 
< base class name>



Programming in java to implement multiple inheritance.

Program in java to implement multiple inheritance

=>

import java.util.*;         /* imports all of the package members from java library. */

class student  /* create  class   and  class name is student*/   
{
int r;         
String nm;  
void read(){    
System.out.println("enter roll no.");          
Scanner sc= new Scanner(System.in);
r = sc.nextInt();
System.out.println("enter name");
rm = sc.next();

class Exam extends student
{
int m;
void read1(){
Scanner sc= new Scanner(System.in);
System.Out.Println("enter markes");
m = sc.nextInt();
}
}
class Result extends Exam
{
int s_m, t();

void read2()
{
Scanner sc = new Scanner(System.in);
System.Out.println("enter sports mark");
s_m = sc.nextInt();
t = m + s_m;
}
void display(){
System.Out.println("name of student:"+ nm + "Roll no. of student:"+ r+ " Total of marks of student:"+t);
}
}

class Sample
{
public static void main(string[]args)
{
Result result=new Result();
result.read();
result.read1();
result.read2();
result.display();
}
}

Code Explanation

Import java.util.*;
The statement java.util.*; imports all of the package members so you don't have to use a package members fully qualified.

class student 
Created a class with student class name.


int 
It will take integer type data from user.

string 
It will take string type data from users.

Void 
This is  empty function which  returns nothing.

System.Out.Println(".......");
It is used to print an argument that is passed to it.

System
It is final class defined in the java.lang.package.

Out
This is an instance of PrintStream type, which is public static field of java.

Println
As all instance of PrintStream class have public method PrintStream class.

Scanner
The scanner class is used to get user input and it is found found in java.util package

nextInt()
The nextInt() method of java scanner class is used to scan the next token of the input as an int.



Class Exam extends student
The extends keyword extends a class (indicates that a class is inherited from another class)

exam   
it's a parent class being inherit

student  
it's a child class inherit from other class



public static void main(string [] args)

public    
It is an access modifier, It specifies from where and who can access the method.

static 
Keyword that is associated with method

void
It is a keyword and is used to specify that method doesn't return anything.

main
Name of java main method


(string[]args)
It stores java command  line arguments and is an array of type java.lang.string class.







Post a Comment

0 Comments