10 What is Constructor in Java

techmanga June 27, 2021 0 Comments

25. What is the difference between an abstract class and an interface?

Abstract class| Interface —|— Abstract class can have both abstract and non-abstract methods| Interface canhave only abstract methods. From Java 8, supports default methods Does not support multiple inheritance| Supports multiple inheritance Uses abstract keyword| Uses interface keyword Uses extends keyword to inherit the abstract class| Uses implements keyword toimplement the interface It can extend another Java class and also implement interface| It can onlyextend another interface Members can have access modifiers like private, protected, etc| Members canhave only public It can have static, non-static, final or non-final variables.| It can haveonly static and final variables

29. How to invoke the superclass version of the overriding method in a

subclass?We can use the super keyword to invoke the superclass version of theoverriding method in a subclass.

30. Can we override a final method in Java?

No we cannot override a final method since we cannot change the logicalimplementations if a method is declared as final. The main idea of finalmethod is to prevent modification.

31. Can we declare non-abstract methods inside an interface?

Yes, from Java 8 we can declare non-abstract methods, static and defaultmethods inside an interface. Prior to Java 8, it does not allow non-abstractmethods.

1. Explain Java Main Method public static void main (String[] args)

When you start learning Java, the first method you encounter is public staticvoid main(String [] args). The starting point of any Java Program is themain() method. It is one of the important methods of Java. Technically, themain method is the starting point where the Java program starts its execution.JVM always look for this method signature to start running an application.Check this to know detailed explanation.

8. What is an Object in Java?

An object is an instance of a class. Objects have state (variables) andbehavior (methods).Example: A dog is an object of Animal class. The dog has its states such ascolor, name, breed, and behaviors such as barking, eating, wagging her tail.|public class MyClass{ //Class name (MyClass) declarationpublic static void main(String[] args){MyClass obj = new MyClass(); //Object Creation}} —|—

9. What is a Class in Java?

A class can be defined as a collection of objects. It is the blueprint ortemplate that describes the state and behavior of an object.|public class MyClass{ //Class name (MyClass) declarationint a = 9; // Variable declarationint b = 99;public void myMethod(){ //Method (myMethod) declarationint sum=a+b;}} —|—

10. What is Constructor in Java?

Constructor in Java is used in the creation of an Object that is an instanceof a Class. The constructor name should be the same as the class name. Itlooks like a method but it’s not a method. It won’t return any value. We haveseen that methods may return a value. If there is no constructor in a class,then the compiler automatically creates a default constructor.

19. What is Abstract Class in Java?

We can easily identify whether a class is an abstract class or not. A classthat contains abstract keyword in its declaration then it is an AbstractClass.Syntax:|abstract class {} —|— Points to remember: * Abstract classes may or may not include abstract methods * If a class is declared abstract then it cannot be instantiated. * If a class has abstract method then we have to declare the class as abstract class * When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.

What is an Object ?

An object is a real world entity. It is an instance of a class.An Object has its own identity, state and behavior.State typically represents data, behavior represents methods and the identityis a unique id associated with object.

Explain the main difference between a class and an object ?

An object is an instance of a class itself. However, a class is the templatewhich have collection of similar objects.> Objects is a real world entity but class is logical. * Class can not hold any information but Objects are able to hold multiple information. * A class can have properties and function but a object cannot. * Object does not have any sub objects concept, but a class can have sub classes.

How many instances can be created for an abstract class ?

None. An abstract class cannot be instantiated.

Leave a Reply

Your email address will not be published. Required fields are marked *