Servlet example programs in eclipse

1.Run eclipse.File -> new -> dynamic web project

Servlet Example in exclipse


2. Give project name : example MyFirstApp and click on next



Servlet Example in exclipse


3. Please check Generate web.xml deployment descriptor to auto generate web.xml file


Servlet Example in exclipse




interview Servlet Example in exclipse

4.click on finish. now project structure will be created.

Servlet Example program  in exclipse

5.Go to webcontent and create a folder with name Jsp to place jsp files and create a index.jsp.


Servlet Example program  in exclipse




Servlet Example program  in exclipse


Servlet Example program  in exclipse





Servlet Example program  in exclipse


6. index.jsp will have an error because it is not having servlet jar. file so we need to include servler-apt.jar file.

Servlet Example program  in exclipse


Servlet Example program  in exclipse



Servlet Example program  in exclipse



now its fine. modify index.jsp with one text box and submit button.


Servlet Example program  in exclipse




  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1
  2.     pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org
  4. /TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  8.  
  9. <title>Login</title>
  10.  
  11. </head>
  12. <body>
  13.  
  14. <form action="/MyFirstApp/hello" method="post">
  15.  
  16.        Name:<input type="text" name="name" value="">
  17.         <input type="submit" name="name" value="submit">
  18.  
  19.  </form>
  20.  
  21. </body>
  22.  
  23. </html>


7. create a servlet


Servlet Example program  in exclipse






Servlet Example program  in exclipse


Servlet Example program  in exclipse


  1. package com.instanceofjava;
  2.  
  3. import java.io.IOException;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8.  
  9. public class Hello extends HttpServlet{
  10.  
  11.   private static final long serialVersionUID = 1L;
  12.  
  13. public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException,
  14. ServletException{
  15.  
  16.         String str= (String) req.getParameter("name");
  17.         req.setAttribute("name", str);
  18.  
  19.         req.getRequestDispatcher("/Jsp/welcome.jsp").forward(req, res);
  20.  
  21.     }
  22. }

8. create a welcome.jsp page.


Servlet Example program  in exclipse


Servlet Example program  in exclipse








  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
  2. pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org
  4. /TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  8.  
  9. <title>welcome</title>
  10. </head>
  11.  
  12. <body>
  13.  
  14. <%
  15. String str=null;
  16.  
  17. try{
  18.  
  19.     str=(String)request.getAttribute("name");
  20.  
  21. }catch(Exception e){
  22.  
  23. }
  24.  
  25. %>
  26.  
  27. <p>Welcome: <%=str %></p>
  28.  
  29. </body>
  30.  
  31. </html>


8. go to we.xml and include our servlet




Servlet Example program  in exclipse



  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns
  4. /javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  5.  <display-name>MyFirstApp</display-name>
  6.  <welcome-file-list>
  7.  
  8.     <welcome-file>/Jsp/index.jsp</welcome-file>
  9.  
  10.   </welcome-file-list>
  11.  
  12.   <servlet>  
  13.  
  14.    <servlet-name>HelloServlet</servlet-name>  
  15.    <servlet-class>com.instanceofjava.Hello</servlet-class> 
  16.  
  17.   </servlet>  
  18.  
  19. <servlet-mapping>
  20.  
  21.         <servlet-name>HelloServlet</servlet-name>
  22.         <url-pattern>/hello</url-pattern>
  23.     </servlet-mapping>
  24.  
  25. </web-app>


9. run the project , select tomcat 7 from apache.

Servlet Example program  in exclipse


10. Enter your name and click on submit.

Servlet Example program  in exclipse


Servlet Example program  in exclipse


Accessibility modifiers examples


  • The keywords which define accessibility permissions are called accessibility modifiers.
  • Java supports four accessibility modifiers to define accessibility permissions at different levels.

Accessibility modifier keywords: 

 1.private

 2.protected

 3.public

 4.default(no keyword)

1.private:

  • The class members which have private keyword in its creation statement are called private members. Those members are only accessible within that class.
  • If we declare any variable or method with private accessibility modifier then those variables and methods are accessible only within that class , not accessible out side the class.

private variable are accessible within the class :


  1. package com.instanceofjava;
  2.  
  3. public class PrivateDemo {
  4.  
  5.     private String first_name;
  6.     private String last_name;
  7.  
  8. void show(){
  9.  
  10.   System.out.println("First Name:="+first_name);
  11.   System.out.println("Last Name:="+last_name);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        PrivateDemo obj= new PrivateDemo ();
  18.  
  19.         obj.first_name="James";
  20.         obj.last_name="Goosling";
  21.         obj.show();
  22.  
  23.     }
  24. }

Output:

  1. First Name:=James
  2. Last Name:=Goosling

private variable are not  accessible out side the class :

  1. package com.instanceofjava;
  2.  
  3. public class PrivateDemo {
  4.  
  5.     private String first_name;
  6.     private String last_name;
  7.  
  8. void show(){
  9.  
  10.   System.out.println("First Name:="+first_name);
  11.   System.out.println("Last Name:="+last_name);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        PrivateDemo obj= new PrivateDemo ();
  18.  
  19.         obj.first_name="James";
  20.         obj.last_name="Goosling";
  21.       
  22.  
  23.     }
  24. }

  1. package instanceofjava;
  2.  
  3. class Demo {
  4.  
  5. public static void main(String[] args){
  6.  
  7. PrivateDemo obj= new PrivateDemo ();
  8.  
  9.         obj.first_name="James"; // ERROR: The field PrivateDemo.first_name is not visible
  10.  }
  11.  
  12. }

private methods are accessible within the class :

  1. package com.instanceofjava;
  2.  
  3. public class PrivateDemo {
  4.  
  5.     private String first_name;
  6.     private String last_name;
  7.  
  8. private void show(){
  9.  
  10.   System.out.println("First Name:="+first_name);
  11.   System.out.println("Last Name:="+last_name);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        PrivateDemo obj= new PrivateDemo ();
  18.  
  19.         obj.first_name="James";
  20.         obj.last_name="Goosling";
  21.         obj.show();
  22.  
  23.     }
  24. }

Output:

  1. First Name:=James
  2. Last Name:=Goosling

private variable are not  accessible out side the class :

  1. package com.instanceofjava;
  2.  
  3. public class PrivateDemo {
  4.  
  5.     private String first_name;
  6.     private String last_name;
  7.  
  8. void show(){
  9.  
  10.   System.out.println("First Name:="+first_name);
  11.   System.out.println("Last Name:="+last_name);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        PrivateDemo obj= new PrivateDemo ();
  18.  
  19.         obj.first_name="James";
  20.         obj.last_name="Goosling";
  21.       
  22.  
  23.     }
  24. }

  1. package instanceofjava;
  2.  
  3. class Demo {
  4.  
  5. public static void main(String[] args){
  6.  
  7. PrivateDemo obj= new PrivateDemo ();
  8.  
  9.         obj.add(); // ERROR: The method add() from the type PrivateDemo is not visible
  10.  }
  11.  
  12. }



  • private variables and methods are accessible inside that class only. If we declare any variable or method as private , not accessible outside the class.

2.protected

  • The class members which have protected keyword in its creation statements are called protected members. Those members can be accessible with in package from all classes, but from out side package only in subclass that too using subclass name or its object.
  • protected variables accessible inside the package anywhere. Outside package accessible only in sub classes.

Same package anywhere:

  1. package com.instanceofjava;
  2.  
  3. public class ProtectedDemo {
  4.  
  5.     protected int a;
  6.     protected int b;
  7.  
  8. protected void show(){
  9.  
  10.   System.out.println("a="+a);
  11.   System.out.println("b="+b);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        ProtectedDemo obj= new ProtectedDemo ();
  18.  
  19.         obj.a=12;
  20.         obj.b=13;
  21.         obj.show();
  22.  
  23.     }
  24. }

Output:

  1. a=12
  2. b=13

Different package subclass:

  1. package com.accesiblitymodifiers;
  2.  
  3. public class Sample extends ProtectedDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.        Sample  obj= new Sample();
  8.  
  9.         obj.a=12;
  10.         obj.b=13;
  11.         obj.show();
  12.  
  13.     }
  14. }

Output:

  1. a=12
  2. b=13

3.public

  • If we declare any variable or method with public access specifier then those members will be accessible to everywhere.


  1. package com.instanceofjava;
  2.  
  3. public class PublicDemo {
  4.  
  5.     public int x;
  6.     public int y;
  7.  
  8. public void show(){
  9.  
  10.   System.out.println("x="+x);
  11.   System.out.println("y="+y);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        PublicDemo obj= new PublicDemo ();
  18.  
  19.         obj.x=1;
  20.         obj.y=2;
  21.         obj.show();
  22.  
  23.     }
  24. }


Output:

  1. x=1
  2. y=2

4.default

  • If we declare any member with no keyword those members are called default members.
  • default members are accessible to package level.
  • Means we can access anywhere in same package but we can not access in out side the package under any condition.
  • So default will acts as public inside package and private out side the package.

Java data types with examples


  1. Primitive data types or Fundamental data types.
  2. Referenced data types or Derived data types.



1.Primitive Data types


1.byte

  1. package com.instanceofjava;
  2.  
  3. public class ByteDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         byte a=10; // a is a variable of type byte holding the value 1;
  8.         byte b=20;// b is a variable of type byte holding the value 2;
  9.         
  10.         byte c= (byte)(a+b);
  11.         System.out.println(c);
  12.  
  13.     }
  14. }

Output:

  1. 3

2.short

  1. package com.instanceofjava;
  2.  
  3. public class ShortDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         short a=1;
  8.         short b=2;
  9.         
  10.         short c= (short)(a+b);
  11.         System.out.println(c);
  12.  
  13.     }
  14. }

Output:

  1. 3

3.int

  1. package com.instanceofjava;
  2.  
  3. public class IntDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         int a=10; // a is a variable of type integer holding the value 10;
  8.         int b=20;// b is a variable of type integer holding the value 20;
  9.         
  10.         int c= a+b;
  11.         System.out.println(c);
  12.  
  13.     }
  14. }

Output:

  1. 30


4.long

  1. package com.instanceofjava;
  2.  
  3. public class LongDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         long a=1234567890;
  8.         long b=1234567890;
  9.  
  10.         long c= a+b;
  11.  
  12.         System.out.println(c);
  13.  
  14.     }
  15. }

Output:

  1. 2469135780


5.float

  1. package com.instanceofjava;
  2.  
  3. public class FloatDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         float a=1234567.8f;
  8.         float b=1234567.8f;
  9.  
  10.         float c= a+b;
  11.  
  12.         System.out.println(c);
  13.  
  14.     }
  15. }

Output:

  1. 2469135.5


7.double

  1. package com.instanceofjava;
  2.  
  3. public class DoubleDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         double a=1234567910112181314151634444422233334491.1d;
  8.         double b=1234567910112181314151634444422233334491.8d;
  9.         double c= a+b;
  10.         System.out.println(c);
  11.  
  12.     }
  13. }

Output:

  1. 2.4691358202243627E39



8.char

  1. package com.instanceofjava;
  2.  
  3. public class CharDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         char a='A';
  8.         char b= 'B';
  9.  
  10.       System.out.println(a);
  11.       System.out.println(b);
  12.       System.out.println(a+b);
  13.  
  14.     }
  15. }

Output:

  1. A
  2. B
  3. 131


9.boolean

  1. package com.instanceofjava;
  2.  
  3. public class BooleanDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         boolean a=true;
  8.         boolean b= false;
  9.  
  10.       System.out.println(a);
  11.       System.out.println(b);
  12.   
  13.  
  14.     }
  15. }

Output:

  1. true
  2. false

1.Referenced Data types

1.class


  1. package com.instanceofjava;
  2.  
  3. public class Sample{
  4.  int a,b; 
  5.  
  6. void add(){
  7. System.out.println(a+b);
  8. }

  9. public static void main(String[] args) {
  10.  
  11.        Sample obj= new Sample();
  12.  
  13.        obj.a=10;
  14.        obj.b=12;

  15.        obj.add();
  16.  
  17.     }
  18. }

Output:

  1. 22

2.Array

  1. package com.instanceofjava;
  2.  
  3. public class ArrayDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         int a[]={1,2,3,4,5,6};
  8.  
  9.         for (int i = 0; i < a.length; i++) {
  10.             System.out.println(a[i]);
  11.         }
  12.   
  13.  
  14.     }
  15. }

Output:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6


Constructors in java





  • Constructors will be executed when the object is created.
  • Constructors should have same name of class name.
  • Executed once per object.
  • Basically used to assign instance variables
  • We can overload constructors.
  • We can call super class constructor form sub class constructor.






  1. package instanceofjava;
  2. class A{
  3. A(){
  4. }
  5. }
  6. }

  • while creating object constructor will be executed so we can assign instance variables to some default values.

  1. package instanceofjava;
  2. class A{
  3. int a,b
  4. A(int x, int y){
  5. a=x;
  6. b=y;
  7. }
  8.  
  9. public static void main(String[] args){
  10.  A a=new A(10,20);

  11. }
  12. }

Types of constructors:

  • There are two types of constructors 
  • Default constructors
  • Parameterized constructor.

Default constructor:

  •  Default constructor will not have any arguments.
  • If we not defined any constructor in our class. JVM automatically defines a default constructor to our class.
  1. package instanceofjava;
  2. class Sample{
  3. int a,b
  4. Sample(){
  5. a=37;
  6. b=46;
  7. }
  8.  
  9. public static void main(String[] args){
  10.  
  11.  Sample obj=new Sample();
  12.   System.out.println(obj.a);
  13.   System.out.println(obj.b);
  14.  
  15. }
  16. }

Output:

  1. 37
  2. 46

Parameterized constructor


  1. package instanceofjava;
  2. class Sample{
  3. int a,b
  4. Sample(int x, int y){
  5. a=x;
  6. b=y;
  7. }
  8.  
  9. public static void main(String[] args){
  10.  
  11.  Sample obj=new Sample(37,46);
  12.   System.out.println(obj.a);
  13.   System.out.println(obj.b);
  14.  
  15. }
  16. }

Output:

  1. 37
  2. 46

Constructor overloading:

  1. package instanceofjava;
  2. class Sample{
  3. int a,b 
  4. Sample(){
  5. this(1,2);
  6.  System.out.println("Default constructor");
  7.  
  8. }
  9. Sample(int x , int y){
  10.  
  11. this(1,2,3); 
  12. a=x;
  13. b=y;
  14.  System.out.println("Two argument constructor");
  15.  
  16. }
  17.  
  18. Sample(int a , int b,int c){
  19.  System.out.println("Three argument constructor")
  20. }
  21. public static void main(String[] args){
  22.  
  23.  Sample obj=new Sample();
  24.   System.out.println(obj.a);
  25.   System.out.println(obj.b);
  26.  
  27. }
  28. }


Output:

  1. Three argument constructor
  2. Two argument constructor
  3. Default argument constructor
  4. 1
  5. 2





This and Super Keywords


  • this keyword used to store current object reference.
  • super keyword used to store super class reference in subclass object.

this keyword:

  • Predefined instance variable to hold current object reference.

It must used explicitly if non-static variable and local variable name is same.

 

 





  1. package com.instanceofjava;
  2.  
  3. public class ThisDemo {
  4.     int a, b;
  5.  
  6.  ThisDemo(int a, int b){
  7.  
  8.         a=a;
  9.         b=b;
  10.  }
  11.  
  12.     public static void main(String[] args){
  13.  
  14.         ThisDemo obj= new ThisDemo(1,2);
  15.         
  16.         System.out.println(obj.a);
  17.         System.out.println(obj.b);
  18. }
  19. }

Output:

  1. 0
  2. 0
  • Without this keyword if variable names are same we can not assign instance variables so this problem will solve by using this keyword

  1. package com.instanceofjava;
  2.  
  3. public class ThisDemo {
  4.     int a, b;
  5.  
  6.  ThisDemo(int a, int b){
  7.  
  8.         this.a=a;
  9.        this.b=b;
  10.  }
  11.  
  12.     public static void main(String[] args){
  13.  
  14.         ThisDemo obj= new ThisDemo(1,2);
  15.         
  16.         System.out.println(obj.a);
  17.         System.out.println(obj.b);
  18. }
  19. }

Output:

  1. 1
  2. 2

Used to invoke current class constructor:

  1. package com.instanceofjava;
  2.  
  3. public class ThisDemo {
  4.     int a, b;
  5.  
  6. ThisDemo(){
  7. System.out.println("Default constructor called");
  8. }

  9.  ThisDemo(int a, int b){
  10.        this();
  11.         this.a=a;
  12.        this.b=b;
  13.  }
  14.  
  15.     public static void main(String[] args){
  16.  
  17.         ThisDemo obj= new ThisDemo(1,2);
  18.         
  19.         System.out.println(obj.a);
  20.         System.out.println(obj.b);
  21. }
  22. }

Output:

  1. Default constructor called
  2. 1
  3. 2

Used to call current class method:

 

  1. package com.instanceofjava;
  2.  
  3. public class Sample{
  4.     int a, b;
  5.  

  6.  Sample(int a, int b){
  7.       
  8.        this.a=a;
  9.        this.b=b;
  10.  }
  11.  
  12. void show(){
  13.  
  14. System.out.println("Show() method called");
  15.    
  16. }
  17.  
  18. void print(){
  19.  
  20.     this.show();
  21.     System.out.println(obj.a);
  22.     System.out.println(obj.b);
  23.  
  24.  }
  25.     public static void main(String[] args){
  26.  
  27.         Sample obj= new Sample(1,2);
  28.         
  29.       
  30.         obj.print()
  31. }
  32. }



Output:

  1. Show() method called
  2. 1
  3. 2

super keyword:

  • Predefined instance variable used to hold super class object reference through sub class object.

 

Used to call super class constructor:



  1. package com.instanceofjava;
  2.  
  3. public class A{
  4.     int a, b;
  5.  
  6.  A(int a, int b){
  7.  
  8.         a=a;
  9.         b=b;
  10. System.out.println("super class constructor called");
  11.  
  12.  }
  13.  
  14. }


  1. package com.instanceofjava;
  2.  
  3. public class B extends A{
  4.     int a, b;
  5.  
  6.  A(int a, int b){
  7.     super(a,b);
  8.         a=a;
  9.         b=b;
  10. }
  11. public static void main(String[] args){
  12.  
  13.         B obj= new B(1,2);
  14.         
  15.     
  16. }
  17.  
  18.  }
  19.  
  20. }

Output:

  1. super class constructor called
  2. 1
  3. 2

Used to call super class methods from subclass:


  1. package com.instanceofjava;
  2.  
  3. public class A{
  4.     int a, b;
  5.  
  6.  A(int a, int b){
  7.  
  8.         a=a;
  9.         b=b;
  10. System.out.println("super class constructor called");
  11.  
  12.  }
  13.  
  14. void add(){
  15. System.out.println("super class method called");
  16. }
  17.  
  18. }


  1. package com.instanceofjava;
  2.  
  3. public class B extends A{
  4.     int a, b;
  5.  
  6.  A(int a, int b){
  7.     super(a,b);
  8.         a=a;
  9.         b=b;
  10. }
  11. void print(){ 
  12. super.add();
  13. System.out.println(this.a);
  14. System.out.println(this.b);
  15.  


  16. }
  17. public static void main(String[] args){
  18.  
  19.         B obj= new B(1,2);
  20.         
  21.        obj.print();
  22. }
  23.  
  24.  }
  25.  
  26. }

Output:

  1. super class method called
  2. 1
  3. 2

Abstraction

  • Abstraction is fundamental principle of modeling. A system model is created at different levels, starting at the higher levels and adding more levels with more details as more is understood about the system. When complete, the model can be viewed at several levels.
  • So abstraction is about,
  • Looking only at the information that is relevant at that time
  • Hiding details so as not to confuse the bigger picture


  • Abstraction is a process of developing a class by hiding or removing non essential details relevant to user. here non essential details means method body and logic 
  • Basically Abstraction provides a contract between a service provider and clients.
  • we can achieve this by using abstract class

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     abstract void add(); // abstract method
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
  • Abstract class can not be instantiated directly.
  • Means we can not create object for abstract class directly

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     abstract void add(); // abstract method
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9.  
  10. public static void main(String[] args){
  11.  
  12. AbstractDemo   obj= new AbstractDemo(); 
  13. // ERROR: Cannot instantiate the type AbstractDemo
  14.  
  15. }
  16. }

Can we declare abstract methods as static?

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     static abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
  • No, we are not allowed to declare method as static . It leads to compile time error.

Can we declare abstract methods as final?


  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     final abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
  • No, we are not allowed to declare method as static . It leads to compile time error.
  • Because it should be inherited to sub class.


Can we declare abstract methods as private?

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     final abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
  • No, we are not allowed to declare method as static . It leads to compile time error.
  • Because it should be inherited to sub class.

 Legal modifiers allowed in combination with abstract

 1.native

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     native abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }

 2.protected

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     protected abstract void add();
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }

 3.public

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.    public abstract void add();
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }



 4.default

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.   abstract void add();
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }


How it works:

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.   abstract void add();
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }


  1. package Abstraction;
  2. public abstract class Example extends AbstractDemo {
  3.  
  4.   abstract void add(){
  5.   System.out.println("this is implemented method in sub class");
  6. }
  7.  
  8.  
  9. }
Select Menu