Sunday, 22 November 2015

INHERITANCE


5. INHERITANCE

Acquiring the properties from one class to another class is called inheritance

                                    (or)

Producing a new class from already existing class is called inheritance

Reusability of  code is main advantage of inheritance

In java Inheritance is achieved by using extends keyword .

Properties with an access specifier – private can’t be inherited.

Example:

class    Parent{

                        String Pname;

                        String FamilyName;

}

class child extends Parent{

String childName;

int childAge;

void printMyName(){

System.out.println(“My name is”+ childName+ “ “+FamilyName);

}

 

Example 2:

package javatuts;

 

 

class person {

   String name;

   String addr;

   int age;

   void setdetails(String name,String addr,int age){

       this.name=name;

       this.addr=addr;

       this.age=age;

   }

   void getdetails(){

       System.out.println("name:"+name);

       System.out.println("address:"+addr);

       System.out.println("age:"+age);

   }

}

package javatuts;

 

 

class employ extends person{

    int id;

    String companyName;

    String companyAddress;

    employ(int id,String name,String addr,int age,String companyName,String companyAddress){

        this.id=id;

        setdetails(name,addr,age);

        this.companyName=companyName;

        this.companyAddress=companyAddress;

    }

    void getEdetails(){

        System.out.println("employ id"+id);

        getdetails();

        System.out.println("companyName:"+companyName);

        System.out.println("company address:"+companyAddress);

    }

}

public class inherit {

    public static void main(String[] args)

    {

        employ e1=new employ(101,"raees","12-madhnagar",29,"softsolutions","20-hyu");

        e1.getEdetails();

       

    }}

Output:

employ id101

name:raees

address:12-madhnagar

age:29

companyName:softsolutions

company address:20-hyu

Sunday, 1 November 2015

Abstraction

4. ABSTRACTION

Providing the essential features without its inner details is called Abstraction

                                                                        (or)

Hiding the internal implementation without affecting outside world

Abstraction provides security.

For example a class contains lots of dataand the user doesn’t need the entire data.

Advantage:

Every User will get their own view of the data according to to his reqt and will not get confused with unnecessary data
 
There are two ways to achieve abstraction in java
  1. Abstract class (0 to 100%)
  2. Interface (100%)

Abstract class
 
package javatuts;
import java.io.*;
abstract class perimeter{
    abstract void peri();
}

public   class rectangle extends perimeter{
 public static  int width=0;
public static int length=0;
   static double rectangle(){
    double area=length*width;
    System.out.println("area of rectangle:"+area);
    return area;
}
 @Override
void peri(){
    int perimeter=2*(length+width);
    System.out.println("perimeter:"+perimeter);
}
    public static void main(String[] args) throws IOException,NumberFormatException{

 perimeter pr=new rectangle();
     
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("enter the value of width:\n");
  
         width=Integer.parseInt(br.readLine());
    
            System.out.println("enter the length value:\n");
   
         length=Integer.parseInt(br.readLine());
   double c= rectangle();
   System.out.println("value"+c);
        pr.peri();
}
}
output:
enter the value of width:
10
enter the length value:
18
area of rectangle:180.0
value180.0
perimeter:56
 
2.Interface:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javatuts;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

interface examp{
    void reverse();
public abstract    boolean isArmstrong(int input);
}
abstract class rever implements examp{
    @Override
    public void reverse(){
        try {
            int num,rnum=0,temp=0;
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("enter the number to reverse:\n");
            num=Integer.parseInt(br.readLine());
            while(num>0){
                temp=num%10;
                rnum=rnum*10+temp;
                num=num/10;
            }
            System.out.println("reversed number:\t"+rnum);
        } catch (IOException ex) {
            Logger.getLogger(rever.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
public  class abstractinter extends rever{

   
    public boolean isArmstrong(int input){
     String inputAsString = input + "";
        int numberOfDigits = inputAsString.length();
        int copyOfInput = input;
        int sum = 0;
        while (copyOfInput != 0) {
            int lastDigit = copyOfInput % 10;
            sum = sum + (int) Math.pow(lastDigit, numberOfDigits);
            copyOfInput = copyOfInput / 10;
        }
        if (sum == input) {
            return true;
        } else {
            return false;
        }  
    }
    public static void main(String[] args) {
        examp e=new abstractinter();
        e.reverse();
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int inputNumber = scanner.nextInt();
        abstractinter ai=new abstractinter();
        boolean result = ai.isArmstrong(inputNumber);
        if (result) {
            System.out.println(inputNumber + " is an armstrong number");
        } else {
            System.out.println(inputNumber + " is not an armstrong number");
        }
    }
 }

output:
enter the number to reverse:
2345
reversed number: 5432
Enter a number: 3
3 is an armstrong number