Polymorphism Is The Ability of Different Objects To Respond in A Unique Way To The

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

25 to 50

25. What is polymorphism and what are the types of it?

=Polymorphism is the ability of different objects to respond in a unique way to the


same message. Polymorphism in Java has two types: Compile
time polymorphism (static binding) and Runtime polymorphism (dynamic binding)

26. What is method overriding?

=we have defined the run method in the subclass as defined in the parent class but it
has some specific implementation. The name and parameter of the method are the
same, and there is IS-A relationship between the classes, so there is method
overriding.

27. What is method overloading?

=In Java, two or more methods may have the same name if they differ in parameters
(different number of parameters, different types of parameters, or both). ...
These methods are called overloaded methods and this feature is called method
overloading.

28. Difference between method overloading and overriding?

= Overriding vs Overloading :-

1. Overriding implements Runtime Polymorphism whereas Overloading implements


Compile time polymorphism.
2. The method Overriding occurs between superclass and subclass. Overloading
occurs between the methods in the same class.
3. Overriding methods have the same signature i.e. same name and method
arguments. Overloaded method names are the same but the parameters are different.

29. What is static and dynamic binding?

= Static binding uses Type information for binding while Dynamic binding uses


Objects to resolve binding. Overloaded methods are resolved (deciding which method
to be called when there are multiple methods with same name) using static
binding while overridden methods using dynamic binding, i.e, at run time.

30. What Is a Generic Type Parameter?


= In a generic type or method definition, a type parameter is a placeholder for a
specific type that a client specifies when they create an instance of the generic type.

31. What Are Some Advantages of Using Generic Types

= Code that uses generics has many benefits over non-generic code:

 Stronger type checks at compile time. A Java compiler applies


strong type checking to generic code and issues errors if the code violates type safety.
...
 Elimination of casts. ...
 Enabling programmers to implement generic algorithms.

32. If a Generic Type Is Omitted When Instantiating an Object, Will the Code Still Compile

= As generics did not exist before Java 5, it is possible not to use them at all. ... The
point to remember is that while backward compatibility and type erasure make it
possible to omit generic types, it is bad practice

33. How Does a Generic Method Differ from a Generic Type?

= A generic method differs from an ordinary method in the same way. ... If an


instance of Type represents a generic type, then it includes an array of types that
represent the type parameters (for generic type definitions) or the type arguments (for
constructed types).

34. What Is Type Inference?

= Type inference refers to the automatic detection of the type of an expression in a


formal language. These include programming languages and mathematical type
systems, but also natural languages in some branches of computer science and
linguistics

35. What Is a Bounded Type Parameter

= There may be times when you'll want to restrict the kinds of types that are allowed to
be passed to a type parameter. ... This is what bounded type parameters are for. To
declare a bounded type parameter, list the type parameter's name, followed by the
extends keyword, followed by its upper bound.

36. Is It Possible to Declared a Multiple Bounded Type Parameter?


= You cannot specify only some of the parameterized types of the generic class as
you declare a generic variable : it would be a compilation error. Either all
parameterized types of the generic class are specified and valid according to the
bounds defined or a raw type should be used

37. What Is a Wildcard Type

= It represents an unknown type. The wildcard can be used in a variety of situations


such as the type of a parameter, field, or local variable; sometimes as a return type.

38. Give the basic methods in File class?

= The File class contains several methods for working with the path name, deleting


and renaming files, creating new directories, listing the contents of a directory, and
determining several common attributes of files and directories. It is an abstract
representation of file and directory pathnames.

39. Why to use FileWriter class and give its advantages .

= Java FileWriter class is used to write character-oriented data to a file. It is character-


oriented class which is used for file handling in java. Unlike FileOutputStream class,
you don't need to convert string into byte array because it provides method to write
string directly.

Void write(char, c): It is used to write char array ...


Void write(String text): It is used to write the stri...
Void flush(): It is used to flushes the data of File...

40. Write a File-Copy program which copies the content of one file to another. Take both the
file names from the user

= // Java program to copy content from

// one file to another

import java.io.*;
import java.util.*;

public class CopyFromFileaToFileb {

public static void copyContent(File a, File b)

throws Exception

FileInputStream in = new FileInputStream(a);

FileOutputStream out = new FileOutputStream(b);

try {

int n;

// read() function to read the

// byte of data

while ((n = in.read()) != -1) {

// write() function to write

// the byte of data

out.write(n);

finally {

if (in != null) {
// close() function to close the

// stream

in.close();

// close() function to close

// the stream

if (out != null) {

out.close();

System.out.println("File Copied");

public static void main(String[] args) throws Exception

Scanner sc = new Scanner(System.in);

// get the source file name

System.out.println(

"Enter the source filename from where you have to read/copy :");

String a = sc.nextLine();

// source file
File x = new File(a);

// get the destination file name

System.out.println(

"Enter the destination filename where you have to write/paste :");

String b = sc.nextLine();

// destination file

File y = new File(b);

// method called to copy the

// contents from x to y

copyContent(x, y);

}
41. How to read data from a file ,using FileReader class?

Code

1. package com.javatpoint;  
2.   
3. import java.io.FileReader;  
4. public class FileReaderExample {  
5.     public static void main(String args[])throws Exception{    
6.           FileReader fr=new FileReader("D:\\testout.txt");    
7.           int i;    
8.           while((i=fr.read())!=-1)    
9.           System.out.print((char)i);    
10.           fr.close();    
11.     }    
12. }    

42. Give usage of BufferedWriter and BufferedReader classes in Java with example

= The "BufferedWriter" class of java supports writing a chain of characters output stream (Text


based) in an efficient way. The Chain-Of-Characters can be Arrays, Strings etc. The
"BufferedReader" class is used to read stream of text from a character based input stream.

43. Write a code to generate database for Criket player using file handling operations

=Link

https://bedeveloper.wordpress.com/core-java/1write-a-java-program-for-a-cricket-player-
the-program-should-accept-the-details-from-user-max-10-player-code-nameruns-innings-
played-and-number-of-times-not-out-the-program-should-contain-fo/
44. Give the applications where design patterns can be applied ?

= Some of the benefits of using design patterns are:

1. Design Patterns are already defined and provides industry standard


approach to solve a recurring problem, so it saves time if we sensibly use the design
pattern. There are many java design patterns that we can use in our java based
projects.
2. Using design patterns promotes reusability that leads to more robust and highly
maintainable code. It helps in reducing total cost of ownership (TCO) of the software
product.
3. Since design patterns are already defined, it makes our code easy to understand
and debug. It leads to faster development and new members of team understand it
easily.

45. Why factory pattern?

=The Factory Method pattern is generally used in the following situations: A class cannot


anticipate the type of objects it needs to create beforehand. A class requires its subclasses to
specify the objects it creates. You want to localize the logic to instantiate a complex object.

46. Explain factory pattern?

= A Factory Pattern or Factory Method Pattern says that just define an interface or abstract


class for creating an object but let the subclasses decide which class to instantiate. In other
words, subclasses are responsible to create the instance of the class.

47. Draw the design Pattern with a context.

=NO

48. Give examples of creational design pattern .

 Abstract Factory pattern: a class requests the objects it requires from


a factory object instead of creating the objects directly.
 Factory method pattern: centralize creation of an object of a specific type
choosing one of several implementations.
49. Design application by applying the factory pattern

=Link

https://www.tutorialspoint.com/design_pattern/factory_pattern.htm

50. Represent and implement a Shape interface which implements Circle ,Square, rectangle
using Fatory pattern

=Same Link as above

You might also like