Advertisement
hitarth_gg

super java

Feb 25th, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. //Class A is referred to as the superclass or base class.
  2. //Class B is referred to as the subclass or derived class.
  3.  
  4. class A {
  5.     //constructor
  6.     public A() {
  7.         super();
  8.         System.out.println("in A");
  9.     }
  10.  
  11.     public A(int n) {
  12.         super();
  13.         System.out.println("in A int n");
  14.     }
  15.  
  16. }
  17.  
  18. class B extends A {
  19.     //constructor
  20.     public B() {
  21.         super();
  22.         System.out.println("in B");
  23.     }
  24.  
  25.     public B(int n) {
  26.         super(n);
  27.         System.out.println("in B int n");
  28.     }
  29. }
  30.  
  31. class C extends A {
  32.     //constructor
  33.     public C() {
  34.         super();
  35.         System.out.println("in B");
  36.     }
  37.  
  38.     public C(int n) {
  39.         this(); // this will execute the default constructor `public C() {`.
  40.         System.out.println("in B int n");
  41.     }
  42. }
  43.  
  44. public class _05_ThisAndSuper {
  45.     public static void main(String[] args) {
  46.         /* ------------------------------------------------------ */
  47.         // without using super()
  48.         B obj = new B(); // → "in A" \n "in B"
  49.  
  50.         B obj2 = new B(2); // → "in A" \n "in B int n"
  51.         // Why is it calling the constructor of the parent class ?
  52.         //The thing is, every constructor in JAVA has a super(); method even if you don't see it.
  53.         // By default, in every constructor the first statement is super();
  54.  
  55.         // super(); means that : call the constructor of the super class (here, class A), but which one? The default one (and not the parameterized one).
  56.  
  57.         //However, if we want to call the parameterized constructor we'll  use super(n); i.e parameterized super. (see below)
  58.  
  59.         /* ------------------------------------------------------ */
  60.         // with super(n);
  61.         B obj4 = new B(2);
  62.         /* Prints :
  63.  
  64.         in A int n
  65.         in B int n
  66.  
  67.         */
  68.  
  69.         /* ------------------------------------------------------ */
  70.  
  71.         // but what is the super class of class A ?
  72.         // by default class A is extended to Object i.e.
  73.         // class A extends Object {..}
  74.  
  75.         /* ------------------------------------------------------ */
  76.         // what if we want to execute both the constructors in a class ?
  77.         // we'll use "this" keyword for that.
  78.         C obj5 = new C(32);
  79.         /* Prints :
  80.  
  81.         in A
  82.         in B
  83.         in B int n
  84.        
  85.         */
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement