Posts

54.import package for parotected data

 /* protected constructor modifier protected method modifier protected variable modifier */ save in p5 folder same directory package p5; public class book { protected int b_id; protected book(int no) { b_id = no; } protected void show() { System.out.println("Show method of book class is called"); System.out.println("Book ID = " + b_id); } } //save in p6 folder same directory package p6; import p5.*; class child extends book { child (int n) { super(n); } void print() { show(); System.out.println("BookID = " + b_id); } } class import_protected_data { public static void main(String[] args) { child obj = new child(7); obj.print(); } } /* OUTPUT: Book ID = 7 BookID = 7 compile & execute -  javac p5/book.java - javac p6/import_protected_data.java - java p6.import_protected_data */

53.import package in another package

 /public class modifier //public method modifier //Save in p2 folder package p2; public class package2 { public void display() { System.out.println("Welcome to Package"); } } //Save in p3 folder package p3; import p2.*; //import p2.package2(class/method); class import_package { public static void main(String[] args) { package2 obj = new package2(); obj.display(); } } OUTPUT: Welcome to Package /* compile:     -  javac p2/package2.java     - j avac p3/import_package.java execute:     -  java p3.import_package */

52.package detail and example

 /* package - package is collaction of class and interfaces. - 'package' keyword is used for declare package in .java file. - There is only one package statement in a .java file. - package statement is the first statement in a .java file -syntax  package package_name; -Example:  package p1; */ package p1; class package1 { public static void main(String[] args) { System.out.println("This my first package"); } } OUTPUT: This my first package /* From parent folder of p1 To compile  javac p1\package1.java To Execute java p1.test */

51.Ststic_nested_class

 // Ststic_nested_class    class outter  {   static void display()   {   System.out.println("display method of outter class");   }   static class inner   {   void show()   {   System.out.println("show method of inner class");   }   }  }   class Ststic_nested_class  {   public static void main(String[] args) {     outter.display();   outter.inner obj_inner = new outter.inner(); //differnt in non-static   obj_inner.show();   }  } OUTPUT: display method of outter class show method of inner class

50. nested_or_inner_class

 /* nested_or_inner_class TYPE   - Non-static nested class   - Ststic nested class   */  // Non-static  nested class  class outter  {   void display()   {   System.out.println("display method of outter class");   }   class inner   {   void show()   {   System.out.println("show method of inner class");   }   }  }   class nested_or_inner_class  {   public static void main(String[] args) {     outter obj = new outter();   obj.display();   outter.inner obj_inner = obj.new inner();   obj_inner.show();   }  } OUTPUT: display method of outter class show method of inner class

49. final class - can not be extanded

 //final class - can not be extanded // final class modifier final class test  { void show() { System.out.println("Show method of final class test is called "); } }  /* class child extends test { THIS IS NOT POSSIBLE IN FINAL CLASS  } */ class final_class { public static void main(String[] args) { test obj = new test(); obj.show(); } } OUTPUT: Show method of final class test is called

48.RTP_using_abstract

 //Runtime polymorphism using abstract class  // abstarct method modifier // abstract method has never body at time of declaration  // body is written in child class  abstract class shape { abstract void draw(); }  class square extends shape { public void draw() { System.out.println("square => draw();"); } } class rectangle extends shape { public void draw() { System.out.println("rectangle => draw();"); } } class circle extends shape { public void draw() { System.out.println("circle => draw();"); } } class RTP_using_abstract { public static void main(String[] args) { shape ref; ref = new square(); ref.draw(); ref = new rectangle(); ref.draw(); ref = new circle(); ref.draw(); } } OUTPUT: square => draw(); rectangle => draw(); circle => draw();