Você está na página 1de 6

Classpath and Path:

PATH is nothing but setting up an environment for operating system. Operating System will look in this PATH for executables. Classpath is nothing but setting up the environment for Java. Java will use to find compiled classes (i.e. .class files). For example,assume that Java is installed in C:\jdk1.5.0\ and your code is in C:\Sample\example.java, then your PATH and CLASSPATH should be set to: PATH=C:\jdk1.5.0\bin;%PATH% CLASSPATH=C:\Sample
Q) Which circumstances you use Abstract Class & Interface? --> If you need to change your design make it an interface. --> Abstract class provide some default behaviour, A.C are excellent candidates inside of application framework. A.C allow single inheritance model, which should be very faster.

Q) Abstract Class Any class that contain one are more abstract methods must also be declared as an abstract, there can be no object of an abstract class, we cannot directly instantiate the abstract classes. A.C can contain concrete methods. Any sub class of an Abstract class must either implement all the abstract methods in the super class or be declared itself as Abstract. Compile time error occur if an attempt to create an instance of an Abstract class. You cannot declare abstract constructor and abstract static method. An abstract method also declared private, native, final, synchronized, strictfp, protected. Abstract class can have static, final method (but there is no use). Abstract class have visibility public, private, protected. By default the methods & variables will take the access modifiers is <default>, which is accessibility as package. An abstract method declared in a non-abstract class. An abstract class can have instance methods that implement a default behavior.

A class can be declared abstract even if it does not actually have any abstract methods. Declaring such a class abstract indicates that the implementation is somehow incomplete and is meant to serve as a super class for one or more subclasses that will complete the implementation. A class with an abstract method. Again note that the class itself is declared abstract, otherwise a compile time error would have occurred.

Abstract class A{ Public abstract callme(); Void callmetoo(){ } }

class B extends A( void callme(){ } }

class AbstractDemo{ public static void main(string args[]){ B b = new B(); b.callme(); b.callmetoo(); } }

Q) When we use Abstract class? A) Let us take the behaviour of animals, animals are capable of doing different things like flying, digging, Walking. But these are some common operations performed by all animals, but in a different way as well. When an operation is performed in a different way it is a good candidate for an abstract method.

Public Abstarctclass Animal{ Public void eat(food food) { } public void sleep(int hours) { } public abstract void makeNoise() }

public Dog extends Animal { public void makeNoise() { System.out.println(Bark! Bark); } }

public Cow extends Animal { public void makeNoise() { System.out.println(moo! moo); } }

Q) Interface Interface is similar to class but they lack instance variable, their methods are declared with out any body. Interfaces are designed to support dynamic method resolution at run time. All methods in interface are implicitly abstract, even if the abstract modifier is omitted. Interface methods have no implementation;

Interfaces are useful for?

a) Declaring methods that one or more classes are expected to implement b) Capturing similarities between unrelated classes without forcing a class relationship. c) Determining an object's programming interface without revealing the actual body of the class.

Why Interfaces? one interface multiple methods signifies the polymorphism concept. Interface has visibility public. Interface can be extended & implemented. An interface body may contain constant declarations, abstract method declarations, inner classes and inner interfaces. All methods of an interface are implicitly Abstract, Public, even if the public modifier is omitted. An interface methods cannot be declared protected, private, strictfp, native or synchronized. All Variables are implicitly final, public, static fields. A compile time error occurs if an interface has a simple name the same as any of it's enclosing classes or interfaces. An Interface can only declare constants and instance methods, but cannot implement default behavior. top-level interfaces may only be declared public, inner interfaces may be declared private and protected but only if they are defined in a class.

A class can only extend one other class. A class may implements more than one interface. Interface can extend more than one interface.

Interface A { final static float pi = 3.14f; }

class B implements A

{ public float compute(float x, float y) { return(x*y); } }

class test{ public static void main(String args[]) { A a = new B(); a.compute(); } }

Q) Diff Interface & Abstract Class? A.C may have some executable methods and methods left unimplemented. Interface contains no implementation code. An A.C can have nonabstract methods. All methods of an Interface are abstract. An A.C can have instance variables. An Interface cannot. An A.C must have subclasses whereas interface can't have subclasses An A.C can define constructor. An Interface cannot. An A.C can have any visibility: public, private, protected. An Interface visibility must be public (or) none. An A.C can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior.

Q) What is the difference between Interface and class? A class has instance variable and an Interface has no instance variables. Objects can be created for classes where as objects cannot be created for interfaces. All methods defined inside class are concrete. Methods declared inside interface are without any body.

Q) What is the difference between Abstract class and Class? Classes are fully defined. Abstract classes are not fully defined (incomplete class) Objects can be created for classes, there can be no objects of an abstract class.

Q) What are some alternatives to inheritance? A) Delegation is an alternative to inheritance. Delegation means that you include an instance of another class as an instance variable, and forward messages to the instance. It is often safer than inheritance because it forces you to think about each message you forward, because the instance is of a known class, rather than a new class, and because it doesnt force you to accept all the methods of the super class: you can provide only the methods that really make sense. On the other hand, it makes you write more code, and it is harder to re-use (because it is not a subclass).

Você também pode gostar