Java Assignment – 2

Ans.

  • Constructor is used to initialize an object whereas method is used to exhibits functionality of an object.

  • Constructors are invoked implicitly whereas methods are invoked explicitly.

  • Constructor does not return any value where the method may/may not return a value.

  • In case constructor is not present, a default constructor is provided by java compiler. In the case of a method, no default method is provided.

  • Constructor should be of the same name as that of class. Method name should not be of the same name as that of class.

Ans.

    1.this

ü  this keyword is a reference variable that refers to the current object.

ü  this can be used to refer current class instance variable.

ü  this can be used to invoke current class method.

       Ã¼ this() can be used to invoke current class constructor.

    2. Super :- 

ü  Super is a keyword.

ü  Constructor which is defining in a subclass is called sub class constructor.

ü  ‘Super’ keyword is used to invoke constructor method of super class.

ü  Super keyword is used to access variable of super class which have same name in sub class.

        Conditions to used ‘super’ keyword: -

ü  Super keyword only defines in subclass constructor method.

ü  In a subclass constructor method we have to first mention the super keyword statement.

ü  The parameter in a super call must be match with the declare parameter in super class. 

3. Static

ü  Static is a member that is common to all object and Exide without using any object.

ü  Static is a keyword.

ü  Static method and variable are called using only with class name.

Syntax:

Classname.variablename
Classname.methodname ( ); 

ü  We can define static variable, static method and static block.

1.      Static variable: static int count;

2.      Static method:   static int max(int x,int y){/*code*/ }                 


3.      static block: static {/*code*/ }                       

        4.Final:
ü  Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final method can't be overridden and final variable value can't be changed

Java final example

    class FinalExample{  
    public static void main(String[] args){  
    final int x=100;  
    x=200;//Compile Time Error  
    }}
3 Explain all visibility access modifiers.

Ans. 

There are two types of modifiers in Java: access modifiers and non-access modifiers.

The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or class. We can change the access level of fields, constructors, methods, and class by applying the access modifier on it.

There are four types of Java access modifiers:

  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.

There are many non-access modifiers, such as static, abstract, synchronized, native, volatile, transient, etc. Here, we are going to learn the access modifiers only.

Ans. 

Method Overloading

Method Overriding

When same method name but different parameters and return type is used in a single class then it is known as Method Overloading.

When same method name, same argument and same return type is used in a sub class and superclass is called Method Overriding.

Method Overloading can be done in same class.

Method Overriding can be done in different class.

In a Method Overloading parameter must be different.

In a Method Overriding parameter must be same.

Method Overloading is the example of compile time Polymorphism.

Method overriding is the example of run time polymorphism.

Return type can be same or different in method Overloading.

Return type must be same in Method overriding.

For implementing method overloading we do not require inheritance.

For implementing method overloading we must require inheritance.

Ans. 

Abstract class Interface
1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods. Since Java 8, it can have default and static methods also.
2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
4) Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class.
5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
6) An abstract class can extend another Java class and implement multiple Java interfaces. An interface can extend another Java interface only.
7) An abstract class can be extended using keyword "extends". An interface can be implemented using keyword "implements".
8) A Java abstract class can have class members like private, protected, etc. Members of a Java interface are public by default.
9)Example:
public abstract class Shape{
public abstract void draw();
}
Example:
public interface Drawable{
void draw();
}

Ans. 

Java finally block is a block that is used to execute important code such as closing connection, stream etc.

Java finally block is always executed whether exception is handled or not.

Java finally block follows try or catch block.

Why use java finally

  • Finally block in java can be used to put "cleanup" code such as closing a file, closing connection etc.

Usage of Java finally

Let's see the different cases where java finally block can be used.

Case 1

Let's see the java finally example where exception doesn't occur.

  1. class TestFinallyBlock{  
  2.   public static void main(String args[]){  
  3.   try{  
  4.    int data=25/5;  
  5.    System.out.println(data);  
  6.   }  
  7.   catch(NullPointerException e){System.out.println(e);}  
  8.   finally{System.out.println("finally block is always executed");}  
  9.   System.out.println("rest of the code...");  
  10.   }  
  11. }  

7 Explain use of throw in exception handling with example.

Ans. 

The Java throw keyword is used to explicitly throw an exception.

We can throw either checked or uncheked exception in java by throw keyword. The throw keyword is mainly used to throw custom exception. We will see custom exceptions later.

The syntax of java throw keyword is given below.

  1. throw exception;  

Let's see the example of throw IOException.

  1. throw new IOException("sorry device error);  

Ans. 

Scene scene = new Scene(Parent); or
Scene scene = new Scene (Parent, Width, Height);

primaryStage.setScene(scene); //assuming you are using the primary stage

Parent.getChildren().add(circle); // the circle must be placed on a pane because the scene cannot support shapes, only controls and panes.

Ans. 

A pane is a container class that allow the setting of the position and size properties of attached nodes.
A node is a visual component such as a shape, image view, UI control or pane.
paneName.getChildren().add(nodeName);
No.
Yes.

Ans. 

  • A binding property is used to bind with a source object. Any changes to the source will be reflected in the bound target.
  • A binding property is an instance of of the interface Property.
  • A source object is an instance of the inteface ObservableValue.
  • The binding object types for int, long, float, double, and boolean are IntegerProperty, LongProperty, DoubleProperty, and BooleanProperty.
  • The classes Integer and Double are not subtypes of ObservableValue. Hence, they cannot be used as a source object in a binding.

Ans. 

You can use the Color constructor or static methods in the Color class to create Color objects. new Color(1.2, 2.3, 3.5, 4) is wrong because the parameter values must be between 0 and 1. new Color(0, 0, 0, 1) is darker than new Color(1, 1, 1, 1). Invoking c.darker() returns a new Color. Color is immutable.

Ans. new Font("Courier", Weight.BOLD, 20)

Ans. Use new Image(filename) or new Image(url)

Ans. Use new ImageView(image)

Ans. You can set an Image to multiple ImageView, but you cannot display one ImageView multiple times.

Ans. To add a node to a Pane, StackPane, FlowPane, HBox, and VBox, use pane.getChildren().add(node). To add node to a BorderPane, use the setTop, setBottom, setLeft, setRight, and setCenter methods. To remove a node from these panes, use pane.getChildren().remove(node).

Ans. To display a text, line, rectangle, circle, ellipse, arc, polygon, and polyline, create an instance of the Text, Line, Rectangle, Circle, Ellipse, Arc, Polygon, and Polyline and add it to a pane and place the pane into a scene.

Ans. The event source object, also called source object, is the source where an event is fired. An event object contains the information about the event. The relationship between a source object and an event is that the event is an object created by the source.

Ans. A functional interface is the interface with exactly one method. A lambda expression works only with a functional interface. For the compiler to understand lambda expressions, the interface must contain exactly one abstract method.

Ans.

animation.setCycleCount(Timeline.INFINITY);
animation.setAutoReverse(true);
animation.start();
animation.pause();
animation.stop();


No comments:

Get new posts by email:


APY Logo