Help shape GoConqr. What should we add next?

Flashcards

Week 2 QC - Revature

Matt Moody English 33 cards

Cards in this deck (33)

  1. 1What is the purpose of inheritance?Is a mechanism in which one object acquires all the properties and behaviors of a parent object. The idea behind inheritance in Java is that you can create new classes that are built upon existing class.
  2. 2What is a subclass/child class?A class that is derived from another class. Modular derivative classes that inherit one or more language entities from one or more other classes.
  3. 3What is a superclass/base class?Through inheritance a new class inherits some existing behavior and states of another class which is called as the superclass. This promotes the concept of code reusability.
  4. 4What does a subclass inherit from the super class?The class that inherits the other class is known as a subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.
  5. 5What is the purpose of super()?The super() in Java is a reference variable that is used to refer parent class constructors. super() can be used to call parent class’ constructors only. Call to super() must be first statement in Derived(Student) Class constructor. If a constructor does not explicitly invoke a superclass constructor by using super(), the Java compiler automatically inserts a call to the no-argument constructor of the superclass.
  6. 6What is method overriding?If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.
  7. 7How is method overriding different than method overloading?Method overloading is used to increase the readability of the program. Method overloading is performed within class. In case of method overloading, parameter must be different. Method overloading is the example of compile time polymorphism. In java, method overloading can't be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter. Method overriding is used to provide the specific implementation of the method that is already provided by its super class. Method overriding occurs in two classes that have IS-A (inheritance) relationship. In case of method overriding, parameter must be same. Method overriding is the example of run time polymorphism. Return type must be same or covariant in method overriding.
  8. 8If Dog extends Animal, can you use Animal reference variable to point to a Dog object?Yes, with upcasting. Upcasting allows the retyping of variables and objects which would allow the parent class to point to a child object.
  9. 9If a method is first defined in the Dog class (not overriden from the Animal class), can we invoke that method from an Animal reference variable? If not, what do we need to do with that Animal reference variable? (hint: starts with down)Yes, but with downcating. Downcasting is used when we need to develop a code that accesses behaviors of the child class. The reference of the parent class object is passed to the child class. Implicitly Downcasting is not possible. The methods and variables of both the classes(parent and child) can be accessed. All the methods and variables of both classes can be accessed by performing downcasting. Parent p = new Child()
    Child c = (Child)p;
  10. 10What is special about the Object class?The Object class is the parent class of all the classes in java by default. In other words, it is the topmost class of java. The Object class is beneficial if you want to refer any object whose type you don't know. Notice that parent class reference variable can refer the child class object, know as upcasting. Object obj=getObject();//we don't know what object will be returned from this method
  11. 11What methods does the Object class contain?1. public final Class getClass() : returns the Class class object of this object. The Class class can further be used to get the metadata of this class.
    2. public int hashCode() : returns the hashcode number for this object.
    3. public boolean equals(Object obj) : compares the given object to this object.
    4. protected Object clone() throws CloneNotSupportedException : creates and returns the exact copy (clone) of this object.
    5. public String toString() : returns the string representation of this object.
    6. public final void notify() : wakes up single thread, waiting on this object's monitor.
    7. public final void notifyAll() : wakes up all the threads, waiting on this object's monitor.
    8. public final void wait(long timeout)throws InterruptedException : causes the current thread to wait for the specified milliseconds, until another thread notifies (invokes notify() or notifyAll() method).
    9. public final void wait(long timeout,int nanos)throws InterruptedException :
  12. 12What is the purpose of overriding the equals() method?Default implementation of equals() class provided by java.lang.Object compares memory location and only return true if two reference variable are pointing to same memory location i.e. essentially they are same object.
    Java recommends to override equals and hashCode method if equality is going to be defined by logical way or via some business logic: example:
    many classes in Java standard library does override it e.g. String overrides equals, whose implementation of equals() method return true if content of two String objects are exactly same
    Integer wrapper class overrides equals to perform numerical comparison etc.
  13. 13If we do not override the equals() method, how does its implementation in the Object class work?The equals() method will just compare the referenced objects but not what the objects contain
  14. 14When overriding the equals() method, why do we also need to override the hashCode() method?As a side note, when we override equals(), it is recommended to also override the hashCode() method. If we don’t do so, equal objects may get different hash-values; and hash based collections, including HashMap, HashSet, and Hashtable do not work properly
  15. 15What access modifiers are there?1. Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
    2. Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.
    3. Protected: The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.
    4. Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.
  16. 16What are the four pillars of OOP?1. Abstraction
    2. Encapsulation
    3. Inheritance
    4. Polymorphism
  17. 17What is abstraction?The concept allows us to hide the implementation from the user but shows only essential information to the user. Using the concept developer can easily make changes and added over time.
    There are the following advantages of abstraction: a) It reduces complexity.
    b) It avoids delicacy.
    c) Eases the burden of maintenance
    d) Increase security and confidentially.
  18. 18What is encapsulation?is a mechanism that allows us to bind data and functions of a class into an entity. It protects data and functions from outside interference and misuse. Therefore, it also provides security. A class is the best example of encapsulation.
  19. 19What is inheritance?The concept allows us to inherit or acquire the properties of an existing class (parent class) into a newly created class (child class). It is known as inheritance. It provides code reusability.
  20. 20What is polymorphism?The word polymorphism is derived from the two words i.e. ploy and morphs. Poly means many and morphs means forms. It allows us to create methods with the same name but different method signatures. It allows the developer to create clean, sensible, readable, and resilient code.
  21. 21Is method overloading compile-time or runtime polymorphism, and why?Compile-time polymorphism. Method Overloading says you can have more than one function with the same name in one class having a different prototype. Function overloading is one of the ways to achieve polymorphism but it depends on technology that which type of polymorphism we adopt. In java, we achieve function overloading at compile-Time.
  22. 22Is method overriding compile-time or runtime polymorphism, and why?The runtime polymorphism can be achieved by method overriding. Java virtual machine determines the proper method to call at the runtime, not at the compile time. It is also called dynamic or late binding. Method overriding says child class has the same method as declared in the parent class. It means if child class provides the specific implementation of the method that has been provided by one of its parent class then it is known as method overriding.
  23. 23What is a Java bean?Java Bean: a Java bean is a class that
    1. Has a no-args constructor
    2. Has getters/setters for its properties
    3. Is serializable (however it is obsolete at this point)
  24. 24What are getters and setters?Getters: used to retrieve the values of the properties of an object
    Setters: used to change the values of the properties of an object
  25. 25What access modifier should I generally be using to achieve encapsulation?Private
  26. 26What is the purpose of the static keyword?The static keyword in Java is used for memory management mainly. We can apply static keyword with variables, methods, blocks and nested classes. The static keyword belongs to the class than an instance of the class. The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc.
    The static variable gets memory only once in the class area at the time of class loading.
  27. 27What can the final keyword be used with?The final keyword in java is used to restrict the user. The java final keyword can be used in many context. Final can be:
    variable
    method
    class
    The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only. We will have detailed learning of these. Let's first learn the basics of final keyword. If you make any variable as final, you cannot change the value of final variable(It will be constant).
  28. 28Is it possible for a class to extend multiple classes?No, in Java it is not possible to extend multiple classes
  29. 29Is it possible for a class to implement multiple interfaces?Yes, this is the workaround for not being able to extend multiple classes
  30. 30In what version of Java was the default keyword introduced for interfaces, and what is the purpose of the default keyword?Java 8. A Java default keyword is an access modifier. If you didn't assign any access modifier to variables, methods, constructors and, classes, by default, it is considered as default access modifier.
    The default access modifier is accessible within the package only.
    Unlike private and protected, we can create a default outer class by not assigning any access modifier to it. In such a case, it not restricted to take class name similar to a program name.
    If you are overriding any method, overridden method (i.e., declared in the subclass) must not be more restrictive. So, the default method or variable can't be allowed to use private access modifier.
  31. 31If I declare a non-static method inside an interface, what implicit modifiers does it have??
  32. 32What are wrapper classes?Wrapper classes: classes that serve as blueprints for objects that wrap up primitive values
    In Java, there is some management of going between primitives to the wrapper objects and vice versa
    Unboxing: the automatic "extraction" of the primitive value from the wrapper object
    Autoboxing: the automatic creation of a wrapper object from a primitive
    The reason we have unboxing and autoboxing is because of how commonly we need to perform math operations or to treat primitives as objects
  33. 33What is Collections API?

Information

Description
Flashcards on Week 2 QC - Revature, created by Matt Moody on 30/10/2021.
Created by
Matt Moody
Views
1
Rating

More on Inheritance

Feedback

What is it about?

What should we add next?