Java Inheritance
28 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the relationship between the Person class and the Employee class in the example code?

  • Person is a child class of Employee
  • Person and Employee are sibling classes
  • Employee is a child class of Person (correct)
  • Employee is a parent class of Person
  • What is the term for when one class inherits multiple classes?

  • Multilevel inheritance
  • Multiple inheritance (correct)
  • Single inheritance
  • Hierarchical inheritance
  • What is the advantage of using inheritance in programming?

  • It makes code more complex
  • It reduces code readability
  • It allows for code reuse (correct)
  • It allows for code obfuscation
  • What is the term for the class that is being inherited from?

    <p>Parent class</p> Signup and view all the answers

    What is the relationship between the Shape class and the Circle, Triangle, and Rectangle classes in the example hierarchy?

    <p>Circle, Triangle, and Rectangle are child classes of Shape</p> Signup and view all the answers

    Why is multiple inheritance not supported in Java?

    <p>Because it can lead to ambiguity</p> Signup and view all the answers

    What is the advantage of using inheritance in the example code?

    <p>It makes the code more expressive</p> Signup and view all the answers

    What is the term for the Employee class in the example code?

    <p>Child class</p> Signup and view all the answers

    What is the purpose of the this keyword in the constructor Student(int rollno,String name,String course)?

    <p>To reuse the constructor with the same parameters</p> Signup and view all the answers

    What will happen if you call this.fee=fee; before this(rollno,name,course); in the constructor Student(int rollno,String name,String course,float fee)?

    <p>It will result in a compile-time error</p> Signup and view all the answers

    What is the purpose of the super keyword in Java?

    <p>To call a method defined in the super class</p> Signup and view all the answers

    What will happen if you try to call a private method of the super class using the super keyword?

    <p>It will result in a compile-time error</p> Signup and view all the answers

    What is the purpose of the this keyword in the method m(S2 obj)?

    <p>To pass the current object as an argument to the method</p> Signup and view all the answers

    What will happen if you call this.m() in the method n()?

    <p>It will compile and run without errors</p> Signup and view all the answers

    What is the purpose of the this keyword in the constructor A(int x)?

    <p>To invoke the current class constructor</p> Signup and view all the answers

    What will happen if you call this() in the default constructor A()?

    <p>It will compile and run without errors</p> Signup and view all the answers

    What is the purpose of the this keyword in the method getA()?

    <p>To return the current class instance</p> Signup and view all the answers

    What will happen if you try to call this() in the method display()?

    <p>It will result in a compile-time error</p> Signup and view all the answers

    What is the purpose of the super keyword in Java?

    <p>To invoke the constructor of the parent class</p> Signup and view all the answers

    What is the output of the following code: class ParentClass { public ParentClass() { System.out.println("Parent Class default Constructor"); } } public class SubClass extends ParentClass { public SubClass() { super(); System.out.println("Child Class default Constructor"); } public static void main(String args[]) { SubClass s = new SubClass(); } }

    <p>Both <code>Parent Class default Constructor</code> and <code>Child Class default Constructor</code> are printed</p> Signup and view all the answers

    What is the purpose of the this keyword in Java?

    <p>To invoke the constructor of the same class</p> Signup and view all the answers

    What is method overriding in Java?

    <p>When a subclass provides a specific implementation for a method that is already provided by its parent class</p> Signup and view all the answers

    What is the output of the following code: class ParentClass { public void disp() { System.out.println("Parent Class method"); } } public class SubClass extends ParentClass { public void disp() { System.out.println("Child Class method"); } public void show() { super.disp(); } public static void main(String args[]) { SubClass s = new SubClass(); s.show(); } }

    <p>Only <code>Parent Class method</code> is printed</p> Signup and view all the answers

    What is the difference between a default constructor and a parameterized constructor in Java?

    <p>A default constructor does not take any parameters, while a parameterized constructor takes parameters</p> Signup and view all the answers

    What is the purpose of constructor chaining in Java?

    <p>To invoke the constructor of the parent class</p> Signup and view all the answers

    What is the output of the following code: class ParentClass { int val = 999; } public class SubClass extends ParentClass { int val = 123; public void disp() { System.out.println("Value is : " + super.val); } public static void main(String args[]) { SubClass s = new SubClass(); s.disp(); } }

    <p>Value is : 999</p> Signup and view all the answers

    What is inheritance in Java?

    <p>A process where a class inherits the properties of another class</p> Signup and view all the answers

    What is the output of the following code: class ParentClass { public void disp() { System.out.println("Parent Class method"); } } public class SubClass extends ParentClass { public void show() { disp(); } public static void main(String args[]) { SubClass s = new SubClass(); s.show(); } }

    <p>Only <code>Parent Class method</code> is printed</p> Signup and view all the answers

    Study Notes

    Inheritance and super Keyword

    • Inheritance allows for code reuse and more intuitive/expressive code.
    • super keyword can be used to:
      • Call the constructor of the parent class (super()).
      • Refers to a variable in the parent class (super.variable_name).
      • Refers to a method of the parent class (super.method_name).

    Examples of super Keyword

    • super() invokes the constructor of the parent class:
    class ParentClass {
        public ParentClass() {
            System.out.println("Parent Class default Constructor");
        }
    }
    
    class SubClass extends ParentClass {
        public SubClass() {
            super(); // calls ParentClass constructor
            System.out.println("Child Class default Constructor");
        }
    }
    
    • super.variable_name refers to a variable in the parent class:
    class ParentClass {
        int val = 999;
    }
    
    class SubClass extends ParentClass {
        int val = 123;
    
        public void disp() {
            System.out.println("Value is : " + super.val); // prints 999
        }
    }
    
    • super.method_name refers to a method of the parent class:
    class ParentClass {
        public void disp() {
            System.out.println("Parent Class method");
        }
    }
    
    class SubClass extends ParentClass {
        public void disp() {
            System.out.println("Child Class method");
        }
    
        public void show() {
            super.disp(); // calls ParentClass method
        }
    }
    

    Review of Inheritance

    • Inheritance allows for code reuse and more intuitive/expressive code.
    • A child class can inherit from a parent class using the extends keyword.
    • A parent class can have multiple child classes.

    Multiple Inheritance

    • Not supported in Java through classes.
    • A class cannot extend more than one class.

    this Keyword

    • this keyword can be used to:
      • Invoke current class method.
      • Invoke current class constructor.
      • Pass as an argument in the method.
      • Pass as an argument in the constructor call.
      • Return current class instance.

    Examples of this Keyword

    • this to invoke current class method:
    class A {
        void m() {
            System.out.println("hello m");
        }
    
        void n() {
            this.m(); // same as m()
        }
    }
    
    • this to invoke current class constructor:
    class A {
        A() {
            System.out.println("hello a");
        }
    
        A(int x) {
            this(); // calls default constructor
            System.out.println(x);
        }
    }
    
    • this to pass as an argument in the method:
    class S2 {
        void m(S2 obj) {
            System.out.println("method is invoked");
        }
    
        void p() {
            m(this);
        }
    }
    
    • this to pass as an argument in the constructor call:
    class B {
        A4 obj;
    
        B(A4 obj) {
            this.obj = obj;
        }
    
        void display() {
            System.out.println(obj.data);
        }
    }
    
    class A4 {
        int data = 10;
    
        A4() {
            B b = new B(this);
            b.display();
        }
    }
    
    • this to return current class instance:
    class A {
        A getA() {
            return this;
        }
    
        void msg() {
            System.out.println("Hello java");
        }
    }
    

    Note: The above examples are just a few illustrations of how this and super keywords can be used in Java.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Description

    This quiz is about the 'super' keyword in Java, its usage, and its role in inheritance. It covers how to invoke the parent class constructor, access variables, and call methods.

    More Like This

    Java super Keyword
    24 questions
    Java Final Keyword
    1 questions

    Java Final Keyword

    CaptivatingKrypton avatar
    CaptivatingKrypton
    Java Inheritance Study Questions
    14 questions
    Héritage en Java
    24 questions

    Héritage en Java

    InvincibleHeliotrope3078 avatar
    InvincibleHeliotrope3078
    Use Quizgecko on...
    Browser
    Browser