Java Package 3

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

How to prevent any method to get overridden in Java?

We can prevent method overriding in Java in 3 ways: By declaring method final in Base class By declaring method in static in Base class By declaring method private in Base class

What is the syntax of inheritance

We can use either extends or implements keyword to implement inheritance in Java. A class extends another class using extends keyword, an interface can extend another interface using extends keyword, and a class can implement an interface using implements keyword in Java.

What is polymorphism?

Polymorphism in Java is a concept by which we can perform a single action in different ways. Polymorphism derived from two Greek words, Poly-means many, morphs-means ways; so polymorphism means many ways.

What happens if parent class and child class is having same variables or methods name?

Super/parent class variables or methods will be hidden in the child/subs class. We can access hidden super class variables or methods in sub class using super keyword

Why cannot private method be overridden

The child class did not inherit the properties of a private method and therefore it cannot be overridden. Moreover, this kind of a method is invisible to all the entities operating outside the class and the call to it is taken care at the compile time itself by making use of Type.

What is the basic difference between inheritance and abstraction?

Abstraction is an object-oriented concept which allows hiding the internal details and displaying only the functionality to the users. On the other hand, inheritance allows code reuse. You can reuse the functionality you have already coded by using inheritance

Does access modifier affect method overloading in Java?

Access modifiers do not affect method overloading, so overloaded methods can have the same or different access levels.

Is it possible to use static method outside class in Java?

In case that is a public static method, we can use it from anywhere outside the class. If that is package-private (default), then we can use only it within that package

What is the basic difference between inheritance and polymorphism?

Inheritance allows, code reusability and the polymorphism is, the occurrence of one function with different form. The basic difference between inheritance and polymorphism is that inheritance allows the already existing code to be reused again in a program, and polymorphism provides a mechanism to dynamically decide what form of a function to be invoked.

What is the basic difference between inheritance and encapsulation?

Inheritance is an object-oriented concept which creates a parent-child relationship. It is one of the ways to reuse the code written for parent class, but it also forms the basis of Polymorphism. On the other hand, encapsulation is an object-oriented concept which is used to hide the internal details of a class e.g. HashMap encapsulate how to store elements and how to calculate has values.

Is it possible to extends a class to more than one class in Java?

No, a class can only extend just one more class in Java. Though every class , also by default extend the java.lang.Object class in Java

Is it possible to override static method in Java?

No, it is not possible to override static method in Java. Static methods are those which can be called without creating objects of class, these are class level methods. On the other hand, in case a subclass is having same method signature as base class then it is known as method overriding. Its execution decided at runtime.

Is it possible to overloaded methods in Java, just by changing return type?

No. it is not valid to consider return type for method overloading.

Can a class extend itself?

No.it is not possible. It does not make sense at all.

What is the difference between Inheritance and Composition?

The composition is a design technique in which your class can have an instance of another class as a field of your class. Inheritance is a mechanism under which one object can acquire the properties and behavior of the parent object by extending a class. In inheritance there is "is-a" relationship, in composition there is "has-a" relationship. In Inheritance, a class lass can extend only one interface, therefore, you can reuse your code only in one class only We can reuse code in multiple class Inheritance provides its features at compile time Composition is easily achieved at runtime We can't reuse code from the final class It allows code reuse even from final classes It exposes both public and protected method of the parent class It doesn't expose. They interact using public interface.

How many types of polymorphism are available in Java?

There are two types of polymorphism is available java. Compile Time Polymorphism (Method overloading) Run-time Polymorphism (Method Overriding)

What is method overloading?

Two or more methods within the same class that share the same name with different parameter list. The overloaded methods must differ in the type and/ or number of their parameters. Overloaded may have different return types. Constructors can also be overloaded.

Why it is not possible to inherit constructor?

When we say that we cannot inherit constructor, it means that a subclass instance cannot be created by making use of any of the superclass constructors. We cannot do this because we do not want the properties of a superclass constructor to be overridden. This would have been possible if inheritance was in the picture, but it is not because doing that would conflict with another concept known as Encapsulation.

Is it possible to overloaded main() methods in Java?

Yes we can overload the main() method which is static. But entry point to JVM will remain the same main() method with below signature. Public static void main(String arg[]){}

Is it possible to overload static method in Java?

Yes, it is possible to overload static method. Java method overloading is one of the feature in OOPs concept which allows us to have two or more methods with same method name with difference in parameters in other words, we can also call this phenomena as compile time polymorphism.

Is it possible to extends an interface to more than one interface in Java?

Yes, unlike classes, an interface can extend more than one interface in Java. There are several example of this behavior in JDK itself. java.util.List interface extends both Collection and Iterable interface to tell that it is a Collection as well as it allows iteration via Iterator.


Kaugnay na mga set ng pag-aaral

2.4.W - Lesson Review: Matter & Periodic Table / Bonding/ Water & pH

View Set

Lisette's NCLEX MED SURG Study #3

View Set

Chapter 3 Gov Test on the President

View Set

Physics Chapter 5: Forces Questions

View Set

OB-GYN: Endometriosis and Adenomyosis

View Set

N326 Final Exam Practice Questions

View Set

Construction Materials Exam 1 Review

View Set

AP world history unit 1-unit 4 (first semester final)

View Set