Sunday, January 18, 2015

2015: Interview Que2- Java related

How to call a method of a class without creating it's instance
  • create a method in inherit class in which you can use base keyword to call the base class method....
public class Person
    {
        protected string ssn = "444-55-6666";
        protected string name = "John L. Malgraine";

        public virtual void GetInfo()
        {
            Console.WriteLine("Name: {0}", name);
            Console.WriteLine("SSN: {0}", ssn);
        }
    }
    class Employee : Person
    {
        public string id = "ABC567EFG";
        public override void GetInfo()
        {
            // Calling the base class GetInfo method: 
            base.GetInfo();
            Console.WriteLine("Employee ID: {0}", id);
        }
    }

    class TestClass
    {
        static void Main()
        {
            Employee E = new Employee();
            E.GetInfo();
        }
    }
Output
Name: John L. Malgraine
SSN: 444-55-6666 
Employee ID: ABC567EFG
  • We can make the method static, a static method belongs to class rather than object of a class. We can invoke static method without creating instance of a class. Please note there are following restrictions on static method- 
    • static method can access static data member and can change the value of it.
    • The static method can not use non static data member or call non-static method directly
    • this and super cannot be used in static context.
Example of static Method
  1. class Student9{  
  2.      int rollno;  
  3.      String name;  
  4.      static String college = "ITS";  
  5.        
  6.      static void change(){  
  7.      college = "BBDIT";  
  8.      }  
  9.   
  10.      Student9(int r, String n){  
  11.      rollno = r;  
  12.      name = n;  
  13.      }  
  14.   
  15.      void display (){System.out.println(rollno+" "+name+" "+college);}  
  16.   
  17.     public static void main(String args[]){  
  18.     Student9.change();  
  19.   
  20.     Student9 s1 = new Student9 (111,"Karan");  
  21.     Student9 s2 = new Student9 (222,"Aryan");  
  22.     Student9 s3 = new Student9 (333,"Sonoo");  
  23.   
  24.     s1.display();  
  25.     s2.display();  
  26.     s3.display();  
  27.     }  
  28. }  

Output

111 Karan BBDIT
222 Aryan BBDIT
333 Sonoo BBDIT

Difference between class and structure
  • In structure have a by default public. In class have a by default private.
  • There is no data hiding features comes with structures. Classes do, private, protected and public.
  • A structure can't be abstract, a class can.
  • Structure cannot be inherited. But class can be inherit.
Structure :

struct struct-name
{
data-type member1;
data-type member2;
data-type membern;
};
struct struct-name v1,v2...vn;

Class :

class class-name
{
private :

data members;
member functions;

public :

data members;
member functions;

protected :

data members;
member functions;

};
class class-name obj1,obj2...objn;

Java.util.Scanner Class

  • The java.util.Scanner class is a simple text scanner which can parse primitive types and strings using regular expressions.
  • A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespac
  • A scanning operation may block waiting for input.
  • This class inherits methods from the following classes: java.util.Object
Popular class constructors
  • Scanner(InputStream source) 
This constructs a new Scanner that produces values scanned from the specified input stream..
e.g. Scanner sc=new Scanner(System.in);
  • Scanner(File source) 
This constructs a new Scanner that produces values scanned from the specified file.
  • Scanner(String source) 
This constructs a new Scanner that produces values scanned from the specified string.

Popular class methods

boolean hasNext() 
This method returns true if this scanner has another token in its input.

int nextInt() 
This method scans the next token of the input as an int.

String next() 
This method finds and returns the next complete token from this scanner..

String nextLine() 
This method advances this scanner past the current line and returns the input that was skipped.

Example to differentiate between next() and nextLine()

String str, str1;
Scanner sc= new Scanner(System.in);
str=sc.next();
        System.out.println(str);
        str1=sc.nextLine();

        System.out.println(str1);

Input#0
www.test.com
www.test1.com
Output#0
www.test.com


String str, str1;
Scanner sc= new Scanner(System.in);
str=sc.nextLine();
        System.out.println(str);
        str1=sc.next();

        System.out.println(str1);

Input#1
my name is test
my name is test1
Output#1
my name is test
my

System.in/System.out/System.err
  • System.in is an InputStream which is typically connected to keyboard input of console programs. 
  • System.out is a PrintStreamSystem.out normally outputs the data you write to it to the console. 
  • System.err is a PrintStreamSystem.err works like System.out except it is normally only used to output error texts.
Here is a simple example that uses System.out and System.err:
try {
  InputStream input = new FileInputStream("c:\\data\\...");
  System.out.println("File opened...");

} catch (IOException e){
  System.err.println("File opening failed:");
  e.printStackTrace();
}


No comments:

Post a Comment