• Nested Class

    A nested class is a class within a class. You may want to create a class within a class for data encapsulation and protect the class from calling from other class.

    Example 1

    package com.javapointers.javacore;
    
    public class ClassA {
    
         public static void main(String args[]){
              ClassB b = new ClassB();
              int ans = b.addNum(10,20);
              System.out.println(ans);
         }
    
    }
    
    class ClassB {
         
         public int addNum(int first, int second){
              int answer = first + second;
              return answer;
         }
    
    }
    
    

    Example 2

    package com.javapointers.javacore;
    
    public class ClassA extends ClassB{
    
         public static void main(String args[]){
              ClassA a = new ClassA();
              String con = a.concatString("Hello ", "Javapointers");
              System.out.println(con);
         }
    }
    
    
    class ClassB {
    
         public String concatString(String a, String b){
              String con = a.concat(b);
              return con;
         }
    }

     

    Related Post