Вы находитесь на странице: 1из 11

Part-A

2.1 What is static method ?


Static methods in java belong to the class (not an instance of it). They use no instance
variables and will usually take input from the parameters, perform actions on it, then return
some result. Instances methods are associated with objects and, as the name implies, can
use instance variables.

2.2 How do you define a class member ?


This is accomplished with the static modifier. Fields that have the static modifier in their
declaration are called static fields or class variables.

2.3 List the access control specifiers in java ?


1. public 2. private 3. protected 4. default

2.4 what is meant by abstract class ?


Abstract classes cannot be instantiated, but they can be subclass. When an abstract class is
subclass, the subclass usually provides implementations for all of the abstract methods in its
parent class.

2.5 Define generic class ?


A type or method to operate on objects of various types while providing compile-time type
safety.

Part-B

2.8 compare static method and fixed method ?


Static methods in java belong to the class (not an instance of it). They use no instance
variables and will usually take input from the parameters, perform actions on it, then return
some result. Instances methods are associated with objects and, as the name implies, can
use instance variables.

Static method in Java is a method which belongs to the class and not to the object. A static
method can access only static data.

It is a method which belongs to the class and not to the object(instance)

A static method can access only static data. It can not access non-static data (instance
variables)
A static method can call only other static methods and can not call a non-static method from
it.

A static method can be accessed directly by the class name and doesn’t need any object

A static method cannot refer to "this" or "super" keywords in anyway

Fixed method

The fixed() method is not standard, and may not work as expected in all browsers. The
fixed() method is used to display a string as teletype text.

2.9 how objects are created in java ?


There are FIVE different ways to create objects in Java:

*Using `new` keyword: This is the most common way to create an object in Java. ...

*By Using Factory Method: ClassName ObgRef=ClassName.

*By Using Cloning Concept:

*Using `Class.forName()`:

*Using object deserialization:

2.10 Explain the uses of keyword super


Java Programming/Keywords/super. It is used inside a sub-class method definition to call a
method defined in the super class. ... Only public and protected methods can be called by
the super keyword. It is also used by class constructors to invoke constructors of its parent
class.

The super keyword in java is a reference variable which is used to refer immediate parent
class object.

Whenever you create the instance of subclass, an instance of parent class is created
implicitly which is referred by super reference variable.

Usage of java super Keyword

super can be used to refer immediate parent class instance variable.

super can be used to invoke immediate parent class method.


super() can be used to invoke immediate parent class constructor.

1) super is used to refer immediate parent class instance variable.

We can use super keyword to access the data member or field of parent class. It is used if
parent class and child class have same fields.

2) super can be used to invoke parent class method

The super keyword can also be used to invoke parent class method. It should be used if
subclass contains the same method as parent class. In other words, it is used if method is
overridden.

3) super is used to invoke parent class constructor.

The super keyword can also be used to invoke the parent class constructor.

2.11 discuss the different levels of access protection available in java

hJava objects have one of four relations to each other. The four relations are:

The objects are in the same class.

One object is a subclass of the other object's class.

The objects are in the same package.

None of the above. (Both objects are members of the general public.)

These relationships are not mutually exclusive. One object can be a subclass of another
object in the same package, for example.

You can define which of your class's members, that is its fields and its methods, are
accessible to other objects in each of these four groups, relative to the current class.

If you want any object at all to be able to call a method or change a field, declare it public.

If you want only objects in the same class to be able to get or set the value of a field or
invoke a method, declare it private.

If you want access restricted to subclasses and members of the same package, declare it
protected.
Finally, to restrict access only to objects in the same package, use no access declaration at
all. This is called "package" or "default" access, but it has no keyword. The default keyword
means something else entirely.

Can anyone remember what?

By default, all classes you write are in the same package. However, they are in different
packages from the Java classes like System or Applet.

The public fields and methods of an object can be accessed from anywhere the object itself
can be seen. Anyone can touch an object's public members. They should be kept to a
minimum. Public fields should relate very closely to the core functionality of the class. They
should not show intimate details of the inner workings of the class. Except in very simple
instances fields should probably not be public.

The private fields and methods of an object can only be accessed by the object itself and by
other objects

2.17 Explain the concepts of method overloading ?


Method Overloading is a feature that allows a class to have more than one method having
the same name, if their argument lists are different. It is similar to constructor overloading
in Java, that allows a class to have more than one constructor having different argument
lists. n such cases we can create methods with the same name and signature as in the
parent class. This way the new method masks the parent method and would get invoked by
default. Overloading in Java is the ability to create multiple methods of the same name, but
with different parameters. Method overloading[edit] ... The practice of defining two or
more methods within the same class that share the same name but have different
parameters is called overloading methods. Methods with the same name in a class are
called overloaded methods.

Overloading means that the same method name can be defined with more than one
signature the list of formal parameters . Overloading means different things in different
languages.

we can create methods with the same name and signature as in the parent class. This way
the new method masks the parent method and would get invoked by default. Overloading
in Java is the ability to create multiple methods of the same name, but with different
parameters. Method overloading. The practice of defining two or more methods within the
same class that share the same name but have different parameters is called overloading
methods. Methods with the same name in a class are called overloaded methods.

Overloading means that the same method name can be defined with more than one
signature ,the list of formal parameters (and their types). Overloading means different
things in different languages. in this way the new method masks the parent method and
would get invoked by default. Overloading in Java is the ability to create multiple methods
of the same name, but with different parameters. Method overloading. The practice of
defining two or more methods within the same class that share the same name but have
different parameters is called overloading methods. Methods with the same name in a class
are called overloaded methods.

2.18 What is the function overloading ? explain with its examples ?

Overloading in Java. Overloading allows different methods to have same name, but different
signatures where signature can differ by number of input parameters or type of input
parameters or both. Overloading is related to compile time (or static) polymorphism. In such
cases we can create methods with the same name and signature as in the parent class. This
way the new method masks the parent method and would get invoked by default.
Overloading in Java is the ability to create multiple methods of the same name, but with
different parameters.

A function/method is sometimes able to take different kinds of parameters in order to do


it's job. This is the time for function overloading. Otherwise, you would have to have
different functions for the same functionality, which is confusing and bad practice.
Constructors are functions, so they can be overloaded. Overloading is related to compile
time (or static) polymorphism. In such cases we can create methods with the same name
and signature as in the parent class. This way the new method masks the parent method
and would get invoked by default. Overloading in Java is the ability to create multiple
methods of the same name, but with different parameters.

A function/method is sometimes able to take different kinds of parameters in order to do


it's job. This is the time for function overloading. Otherwise, you would have to have
different functions for the same functionality

Rules in function overloading[edit]

The overloaded function must differ either by the arity or data types

The same function name is used for various instances of function call,

It is a classification of static polymorphism in which a function call is resolved using the 'best
match technique', i.e. the function is resolved depending upon the argument list. Method
overloading is usually associated with statically-typed programming languages that enforce
type checking in function calls. When overloading a method, you are really just making a
number of different methods that happen to have the same name. The determination of
which of these methods are used is resolved at compile time.
Function overloading is also known as compile-time polymorphism in Java and is also known
as static polymorphism in Java.

Method overloading should not be confused with forms of polymorphism where the correct
method is chosen at runtime, e.g. through virtual functions, instead of statically.

Example: function overloading in C++

#include <iostream>

// volume of a cube

int volume(const int s)

return s*s*s;

// volume of a cylinder

double volume(const double r, const int h)

return 3.1415926*r*r*static_cast<double>(h);

// volume of a cuboid

long volume(const long l, const int b, const int h)

return l*b*h;

}
int main()

std::cout << volume(10);

std::cout << volume(2.5, 8);

std::cout << volume(100, 75, 15);

return 0;

n some programming languages, function overloading or method overloading is the ability


to create multiple methods of the same name with different implementations. Calls to an
overloaded function will run a specific implementation of that function appropriate to the
context of the call, allowing one function call to perform different tasks depending on
context.

For example, doTask() and doTask(object O) are overloaded methods. To call the latter, an
object must be passed as a parameter, whereas the former does not require a parameter,
and is called with an empty parameter field. A common error would be to assign a default
value to the object in the second method, which would result in an ambiguous call error, as
the compiler wouldn't know which of the two methods to use.

Another appropriate example would be a Print(object O) method. In this case one might like
the method to be different when printing, for example, text or pictures. The two different
methods may be overloaded as Print(text_object T);

2.19 what are the difference between method and constructor ?

difference between constructors and methods are:

Constructors create and initialize objects that don't exist yet, while methods perform
operations on objects that already exist.
Constructors can't be called directly; they are called implicitly when the new keyword
creates an object. Methods can be called directly on an object that has already been created
with new.

The definitions of constructors and methods look similar in code. They can take parameters,
they can have modifiers (e.g. public), and they have method bodies in braces.

Constructors must be named with the same name as the class name. They can't return
anything, even void (the object itself is the implicit return).

Methods must be declared to return something, although it can be void.

The important difference between constructors and methods are:

Constructors create and initialize objects that don't exist yet, while methods perform
operations on objects that already exist.

Constructors can't be called directly; they are called implicitly when the new keyword
creates an object. Methods can be called directly on an object that has already been created
with new.

The definitions of constructors and methods look similar in code. They can take parameters,
they can have modifiers (e.g. public), and they have method bodies in braces.

Constructors must be named with the same name as the class name. They can't return
anything, even void (the object itself is the implicit return).

Methods must be declared to return something, although it can be void.

2. Order of execution of constructors in inheritance relationship is from base /parent class


to derived / child class.

We know that when we create an object of a class then the constructors get called
automatically.

In inheritance relationship, when we create an object of a child class, then first base class
constructor and then derived class constructor get called implicitly.
20 explain the sequence of constructor calling in multilevel hierarchy

When a class extends a class, which extends anther class then this is called multilevel
inheritance. For example class C extends class B and class B extends class A then this type of
inheritance is known as multilevel inheritance.

It’s pretty clear with the diagram that in Multilevel inheritance there is a concept of grand
parent class. If we take the example of this diagram, then class C inherits class B and class B
inherits class A which means B is a parent class of C and A is a parent class of B. So in this
case class C is implicitly inheriting the properties and methods of class A along with class B
that’s what is called multilevel inheritance.

To learn the basics of inheritance refer this tutorial: Inheritance in Java

Multilevel Inheritance Example

In this example we have three classes – Car, Maruti and Maruti800. We have done a setup –
class Maruti extends Car and class Maruti800 extends Maruti. With the help of this
Multilevel hierarchy setup our Maruti800 class is able to use the methods of both the
classes (Car and Maruti).

class Car{

public Car()

System.out.println("Class Car");

public void vehicleType()

System.out.println("Vehicle Type: Car");

class Maruti extends Car{

public Maruti()
{

System.out.println("Class Maruti");

public void brand()

System.out.println("Brand: Maruti");

public void speed()

System.out.println("Max: 90Kmph");

public class Maruti800 extends Maruti{

public Maruti800()

System.out.println("Maruti Model: 800");

public void speed()

System.out.println("Max: 80Kmph");

public static void main(String args[])

Maruti800 obj=new Maruti800();

obj.vehicleType();
obj.brand();

obj.speed();

Output:

Class Car

Class Maruti

Maruti Model: 800

Vehicle Type: Car

Brand: Maruti

Max: 80Kmph

Вам также может понравиться