Heading

Thursday, September 19, 2013

Abstract Class Example in Java

 

Example:
abstract class MyClass
{
 public  abstract void display();
}

class FirstClass extends MyClass
{
 public void display()
 {
  System.out.println("method of first class");
 }
 public void firstMethod()
 {
  System.out.println("method of first class first method");
 }
 public void firstMethod1()
 {
  System.out.println("method of first class first method1");
 }
}

public class AbstractCheck extends FirstClass
{
 public void display()
 {
  System.out.println("method of AbstractCheck class");
 }
 public void firstMethod()
 {
  System.out.println("method of AbstractCheck class first method");
 }
 public void secondMethod()
 {
  System.out.println("method of AbstractCheck  class second method");
 }
 public static void main(String... s)
 {
  AbstractCheck mc=new AbstractCheck();
  mc.display();
  mc.firstMethod();
  mc.firstMethod1();
  mc.secondMethod();


 }
}

Output:
G:\company\java programs>javac AbstractCheck.java

G:\company\java programs>java AbstractCheck
method of AbstractCheck class
method of AbstractCheck class first method
method of first class first method1
method of AbstractCheck  class second method


Note:-  The methods which are available in reference class are only allowed you to access with the reference.

public static void main(String... s)
 {
  FirstClass mc=new AbstractCheck();
  mc.display();
  mc.firstMethod();
  mc.firstMethod1();
  mc.secondMethod();
 }

will cause you compile time error because scondMethod() does not exist in FirstClass

G:\company\java programs>javac AbstractCheck.java
AbstractCheck.java:42: error: cannot find symbol
  mc.secondMethod();
    ^
  symbol:   method secondMethod()
  location: variable mc of type FirstClass
1 error

Reading String and integer using scanner

import java.util.Scanner;
public class CharIndex
{
 public static void main(String... s)
 {
 Scanner sc=new Scanner(System.in);

 System.out.println("Reading String with Spaces");
 System.out.println("Enter your String:");
 String str=sc.nextLine();
 System.out.println("Entered String are:"+str+"\n");

 System.out.println("Reading Integer Value");
 System.out.println("Enter your Number:");
 int num=sc.nextInt();
 System.out.println("Entered Number are:"+num);

 System.out.println("Reading String up to Space");
 System.out.println("Enter your String:");
 String str1=sc.next();
 System.out.println("Entered String are:"+str1+"\n");



 }
}

Output:


G:\company\java programs>javac CharIndex.java

G:\company\java programs>java CharIndex
Reading String with Spaces
Enter your String:
v v v veera
Entered String are:v v v veera

Reading Integer Value
Enter your Number:
143
Entered Number are:143
Reading String up to Space
Enter your String:
amrutha m
Entered String are:amrutha

How to Run Java program in Notepad++

Hi Friends

After Writing Java Program, u can run it from notepad++ directly. Simple u just fallow the steps as shown below:
Step1:Write the java program and save it.


Step2: Select Run in the menu and select Open Current dir cmd as shown below


Setp3 : Then on cmd window will open and type commands as fallow:


G:\company\java programs>javac CharIndex.java

G:\company\java programs>java CharIndex
Enter your String:
Power Star
Entered String are:Power Star