Java Flashcards
What are the three key features of OOP and their advantages?
Encapsulation = Robustness & Reliability. Inheritance = Reuse & Interface Extension. Polymorphism = Implementation Reuse.
What is Encapsulation and it’s advantage using examples?
Encapsulation is to bind related functionality (Methods) & Data (Variables) in a protective wrapper (Class) with required access modifiers (public, private, default & protected). The class then provides a well defined interface to a set of functions to access and manipulate the data. This practice allows for a robust and reliable system where unauthorized access is controlled and internal workings are hidden from it’s users making it easier to maintain.
What is Inheritance and it’s advantage using examples?
Inheritance allows us to derive new classes from existing classes. A subclass inherits all the members (fields, methods, and nested classes) from its superclass. The inherited methods can be overridden with a new implementation or new methods added. The advantage of inheritance is code reuse and interface extension which means that the fields and methods of the existing class can be used without having to re-write and re-debug them.
What is Polymorphism and it’s advantage using examples?
Polymorphism is the ability of an object to take on many forms. For example when a parent class reference is used to refer to a child class object. The advantage of polymorphism is implementation reuse because of the objects ability to morph.
What is an Abstract Class, provide an example?
The purpose of an abstract class is to function as a base for subclasses. It may include both abstract and non-abstract methods, and all types of variables. Abstract classes cannot be instantiated, meaning you cannot create new instances of an abstract class.
What is an Interface, provide an example?
An interface is similar to a class but the body of an interface can include only abstract methods and final fields (constants). Interfaces cannot be instantiated and a class must implement an interface by providing code for each method declared by the interface.
What is an Overloading, provide an example?
Overloading is to add a new method with the same name as an inherited method but with a different signature (Static binding at compile time).
What is an Overriding, provide an example?
Overriding is to redefine an inherited method which have the same signature (dynamic runtime at runtime).
What is composition and how does it differ from inheritance?
Composition is when instances of other classes and primitives are members of the class. This differs to inheritance which holds an “is a” relationship and composition holds an “has a” relationship.
What are the four types of access modifiers?
Public which can be accessed from everywhere in the program, Private from inside the same class, Package from anywhere inside the package and Protected from a subclass and same package that contains the protected member.
What is a checked exception?
Describes a problem that is out of our control (i.e. disk failure) and the compiler handles them at compile time.
What is an unchecked exception?
Describes problems in the programmers code and these are not checked by the compiler.
What is the difference between a checked and unchecked exception?
Checked exceptions must be handled whereas unchecked exceptions may be ignored.
What is parametric polymorphism?
This is another term for Generics.
What is a method signature?
It is the method name and parameter list.