Giter Site home page Giter Site logo

win32ramnit / java-interview-questions Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 6 KB

Core Java interview questions and answers for both experience & fresher

core-java core-java-interview-problems interview interview-questions java java-8 java-interface java-interview-questions

java-interview-questions's Introduction

Java Interview Questions and Answers

Core Java interview questions for both experience & fresher


Question 1:

We cannot create an object for an abstract class, but an abstract class can contain a constructor. What is the need for it?

Answer:

The constructor of an abstract class is responsible for initializing the member variables of a child object, specifically those instance variables that are inherited from the parent class. For exmaple:
class Employee {
    private String name;
    private double salary;
    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }
}

class Manager extends Employee {
    private double bonus;
    public Manager(String name, double salary, double bonus) {
        super(name, salary); // calling Employee's constructor
        this.bonus = bonus;
    }
}

class Test {
    public static void main(String[] args) {
        Manager tony = new Manager("Tony", 800000.0, 15000.0);
    }
}

Whenever we create a child class (manager class) object, the parent constructor (Employee's constructor, referred to as 'super' in the child class) is automatically executed to perform initialization for the instance variables that are inherited from the parent class.

Question 2:

What is the need of executing parent's constructor?

Answer:

See answer in the Question 1

Question 3:

Is it true that a parent class object is automatically created whenever we create a child class object?

Answer:

No, it is not true that a parent class object is automatically created whenever we create a child class object. Each class, including the parent and child classes, has its own separate object. However, the child class object can inherit certain properties and behaviors from the parent class. Let see with this example:
class Parent {
    public Parent() {
        System.out.println(this.hashCode()); // 366712642
    }
}

class Child extends Parent {
    public Child() {
    	System.out.println(this.hashCode()); // 366712642
    }
}

public class Test {
    public static void main(String[] args) {
    	Child child = new Child();
        System.out.println(child.hashCode()); // 366712642
    }
}
OUTPUT: 
366712642
366712642
366712642

As we can see, both the child and parent produce the same hashcode. This means that whenever we create a child object, the parent object will not be created. Hence, it is proven.

java-interview-questions's People

Contributors

win32ramnit avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.