Object-Oriented Programming and Java

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What are the types of inheritance?

1). Single Inheritance : One class is extended by only one class. 2). Multilevel Inheritance : One class is extended by a class and that class in turn is extended by another class thus forming a chain of inheritance. 3). Hierarchical Inheritance : One class is extended by many classes. 4).Hybrid Inheritance : It is a combination of above types of inheritance. 5). Multiple Inheritance : One class extends more than one classes. (Java does not support multiple inheritance.)

What is a Constructor?

A constructor gets invoked when a new object is created. Every class has a constructor. In case the programmer does not provide a constructor for a class, the Java compiler (Javac) creates a default constructor for that class.

What is the difference between processes and threads ?

A process is an execution of a program, while a Thread is a single execution sequence within a process. A process can contain multiple threads. A Thread is sometimes called a lightweight process.

What are the differences between Abstraction and Encapsulation?

Abstraction and encapsulation are complementary concepts. On the one hand, abstraction focuses on the behavior of an object. On the other hand, encapsulation focuses on the implementation of an object's behavior. Encapsulation is usually achieved by hiding information about the internal state of an object and thus, can be seen as a strategy used in order to provide abstraction.

Why is Java called the 'Platform Independent Programming Language'?

Platform independence means that execution of your program does not dependent on type of operating system(it could be any : Linux, windows, Mac ..etc).

Can a top level class be private or protected?

Top level classes in java can't be private or protected, but inner classes in java can. The reason for not making a top level class as private is very obvious, because nobody can see a private class and thus they can not use it. Declaring a class as protected also doesn't make any sense. The only difference between default visibility and protected visibility is that we can use it in any package by inheriting it.

What are pass by reference and pass by value?

When an object is passed by value, this means that a copy of the object is passed. Thus, even if changes are made to that object, it doesn't affect the original value. When an object is passed by reference, this means that the actual object is not passed, rather a reference of the object is passed. Thus, any changes made by the external method, are also reflected in all places.

When does an Object becomes eligible for Garbage collection in Java?

A Java object is subject to garbage collection when it becomes unreachable to the program in which it is currently used.

What is encapsulation?

Encapsulation is referred to one of the following two notions. 1) Data hiding: A language feature to restrict access to members of an object. For example, public, private and protected members. 2) Bundling of data and methods together: Data and methods that operate on that data are bundled together.

What is Inheritance?

Inheritance provides an object with the ability to acquire the fields and methods of another class, called base class. Inheritance provides re-usability of code and can be used to add additional features to an existing class, without modifying it.

What is Autoboxing and Unboxing ?

Autoboxing is the automatic conversion made by the Java compiler between the primitive types and their corresponding object wrapper classes. For example, the compiler converts an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this operation is called unboxing.

What is Inheritance?

Inheritance is one of the key features of object oriented programming. Through inheritance, a class (Sub Class) can inherit properties of another class (Super Class). Sub class can have it's own properties along with the inherited properties from it's super class.

Can a class extend itself?

No, A class can not extend itself.

Are constructors and initializers also inherited to sub classes?

No, Constructors and initializers (Static initializers and instance initializers) are not inherited to sub classes. But, they are executed while instantiating a sub class.

Are static members inherited to sub classes?

Yes, Static members are also inherited to sub classes.

What is JVM ? Why is Java called the "Platform Independent Programming Language" ?

A Java virtual machine (JVM) is a process virtual machine that can execute Java bytecode. Each Java source file is compiled into a bytecode file, which is executed by the JVM. Java was designed to allow application programs to be built that could be run on any platform, without having to be rewritten or recompiled by the programmer for each separate platform. A Java virtual machine makes this possible, because it is aware of the specific instruction lengths and other particularities of the underlying hardware platform.

What is Abstraction?

Abstraction is the process of separating ideas from specific instances and thus, develop classes in terms of their own functionality, instead of their implementation details. Java supports the creation and existence of abstract classes that expose interfaces, without including the actual implementation of all methods. The abstraction technique aims to separate the implementation details of a class from its behavior.

What would be the correct way of inheriting class A by class B?

Class B extends A{}

Can private members of a class be inherited by a sub class?

No, private members of a class cannot be inherited by a sub class.

What is Polymorphism?

Polymorphism is the ability of programming languages to present the same interface for differing underlying data types. A polymorphic type is a type whose operations can also be applied to values of some other type.

What happens if both, super class and sub class, have a field with same name.?

Super class field will be hidden in the sub class. You can access hidden super class field in sub class using super keyword.

How do you implement multiple inheritance in java?

Through interfaces, we can implement multiple inheritance in java. As classes in java can not extend more than one classes, but a class can implement more than one interfaces.

Can you access non static variable in static context ?

A static variable in Java belongs to its class and its value remains the same for all its instances. A static variable is initialized when the class is loaded by the JVM. If your code tries to access a non-static variable, without any instance, the compiler will complain, because those variables are not created yet and they are not associated with any instance.

What will happen if you put System.exit(0) on try or catch block? Will finally block execute?

By Calling System.exit(0) in try or catch block, we can skip the finally block. System.exit(int) method can throw a SecurityException. If Sysytem.exit(0) exits the JVM without throwing that exception then finally block will not execute. But, if System.exit(0) does throw security exception then finally block will be executed.

What are main features of OOP?

Encapsulation Polymorphism Inheritance

Explain Final keyword in java?

Final keyword in java is used to restrict usage of variable, class and method. Variable: Value of Final variable is constant, you can not change it. Method: you can't override a Final method. Class: you can't inherit from Final class.

Does java support copy constructors?

Java does support copy constructors like C++, but the difference lies in the fact that Java doesn't create a default copy constructor if you don't write your own.

What is the difference between an Interface and an Abstract class ?

Java provides and supports the creation both of abstract classes and interfaces. Both implementations share some common characteristics, but they differ in the following features: All methods in an interface are implicitly abstract. On the other hand, an abstract class may contain both abstract and non-abstract methods. A class may implement a number of Interfaces, but can extend only one abstract class. In order for a class to implement an interface, it must implement all its declared methods. However, a class may not implement all declared methods of an abstract class. Though, in this case, the sub-class must also be declared as abstract. Abstract classes can implement interfaces without even providing the implementation of interface methods. Variables declared in a Java interface is by default final. An abstract class may contain non-final variables. Members of a Java interface are public by default. A member of an abstract class can either be private, protected or public. An interface is absolutely abstract and cannot be instantiated. An abstract class also cannot be instantiated, but can be invoked if it contains a main method.

What is Function Overriding and Overloading in Java ?

Method overloading in Java occurs when two or more methods in the same class have the exact same name, but different parameters. On the other hand, method overriding is defined as the case when a child class redefines the same method as a parent class. Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides.

Can a class extend more than one class or does java support multiple inheritance? If not, why?

No, a class in java can not extend more than one classes or java does not support multiple inheritance. To avoid ambiguity, complexity and confusion, java does not supports multiple inheritance. For example, If Class C extends Class A and Class B which have a method with same name, then Class C will have two methods with same name. This causes ambiguity and confusion for which method to use. To avoid this, java does not supports multiple inheritance.

What is Object Oriented Programming?

Object Oriented Programming (OOP) is a programming paradigm where the complete software operates as a bunch of objects talking to each other. An object is a collection of data and methods that operate on its data.

Difference in Set and List interface?

Set and List both are child interface of Collection interface. There are following two main differences between them List can hold duplicate values but Set doesn't allow this. In List interface data is present in the order you inserted but in the case of Set insertion order is not preserved.

What is the difference between StringBuffer and String?

String is an Immutable class, i.e. you can not modify its content once created. While StringBuffer is a mutable class, means you can change its content later. Whenever we alter content of String object, it creates a new string and refer to that,it does not modify the existing one. This is the reason that the performance with StringBuffer is better than with String.

What is the Difference between JDK and JRE ?

The Java Runtime Environment (JRE) is basically the Java Virtual Machine (JVM) where your Java programs are being executed. It also includes browser plugins for applet execution. The Java Development Kit (JDK) is the full featured Software Development Kit for Java, including the JRE, the compilers and tools (like JavaDoc, and Java Debugger), in order for a user to develop, compile and execute Java applications.

What is Constructor Overloading?

The constructor overloading is similar to method overloading in Java. Different constructors can be created for a single class. Each constructor must have its own unique parameter list.

What are the Data Types supported by Java ?

The eight primitive data types supported by the Java programming language are: byte, short, int, long, float, double, boolean, char

What is finalize() method?

The finalize method is called by the garbage collector, just before releasing the object's memory. It is normally advised to release resources held by the object inside the finalize method.

What are advantages to Encapsulation?

The internal state of every objected is protected by hiding its attributes. It increases usability and maintenance of code, because the behavior of an object can be independently changed or extended. It improves modularity by preventing objects to interact with each other, in an undesired way.

What is the main advantages of OOP?

The main advantage of OOP is better manageable code that covers following. 1) The overall understanding of the software is increased as the distance between the language spoken by developers and that spoken by users. 2) Object orientation eases maintenance by the use of encapsulation. One can easily change the underlying representation by keeping the methods same. OOP paradigm is mainly useful for relatively big software.

What is the purpose of garbage collection in Java, and when is it used ?

The purpose of garbage collection is to identify and discard those objects that are no longer needed by the application, in order for the resources to be reclaimed and reused.

What does the "static" keyword mean ? Can you override private or static method in Java ?

The static keyword denotes that a member variable or method can be accessed, without requiring an instantiation of the class to which it belongs. A user cannot override static methods in Java, because method overriding is based upon dynamic binding at runtime and static methods are statically binded at compile time. A static method is not associated with any instance of a class so the concept is not applicable.

What is covariant return type?

co-variant return type states that return type of overriding method can be subtype of the return type declared in method of superclass.

When is the super keyword used?

super keyword is used to refer: immediate parent class constructor, immediate parent class variable, immediate parent class method. Refer this for details.

What is the difference between 'throw' and 'throws' in Java Exception Handling?

throw keyword is used to throw Exception from any method or static block whereas throws is used to indicate that which Exception can possibly be thrown by this method E.g. throw throw new Exception("You have some exception") throw new IOException("Connection failed!!") throws throws IOException, NullPointerException, ArithmeticException


Kaugnay na mga set ng pag-aaral

MIDTERM- Comparative Health Politics

View Set

abdominal 1 pathology key pearls FINAL

View Set

Cengage Windows Server 2019 - Module 5 - Configuring Resource Access (Exam Notes)

View Set

Final Exam Review for Foundations

View Set