08 Nov
08Nov
 Abstract Class
  • An abstract class can never be instantiated, it sole purpose in life is to be extended.
  • Note that the methods marked abstract end in semicolon rather than curly braces.
  • Even if a single method is abstract the whole class is abstract.
  • Coding with abstract class types including interfaces lets you take the advantage of polymorphism and gives you the greatest degree of flexibility and extensibility.
  • An abstarct class must be subclassed whereas a final class must not be subclassed. Hence, combination of both of these is never used.
interfaces
  • An abstract class can define both abstract and nonabstract methods,  an interface can have only abstract methods.
  • All interface methods are implicitly public and abstract.
  • All variables defined in an interface must be public , static, and final - in other words , interfaces can only declare constants, not instance variables.
  • Interface method must not be static.
  • Because interface methods are abstract, they cannot be marked final, strictfp, or native.
  • An interface can extend one or more interfaces.
  • An interface cannot extend anything but another interfaces.
  • An interface cannot implement another interface or class.


Final variable
Declaring a variable with the final keyword makes its impossible to reassign that variable once it has been initialized with an explicit value.
Transient Variable
If you mark an instance variable as transient, you are telling the JVM to skip this variable when you attempt to serialize the object containing it.
Enum
  • An enum specifies a list of constant values assigned to a type.
  • An enum is NOT a String or a int; an enum constant type is the enum type. For ex. SUMMER and FALL are of the enum type Season.
  • An enum can be declared outside or inside a class, but NOT in a method.
  • An enum declared outside a class must NOT be marked static, final, abstract, protected, or private.
  • enums can contain constructors, methods, variable, and constant-specific clas bodies.
Inheritance
The common reasons to use inheritance are:
  •      To promote code reuse.
  •      To use polymorphism.



package com.core;
/*
* The methods you can call on a reference are totally dependent on the declared type of the variable, no matter what the
* actual object is, that the reference is referring to. That means you can't use a GameShape variable to call, say, the getAdjacent() method
* even if the object passed in is of type TilePiece.
*/
public class TestShapes {
public static void main(String[] args) {
PlayerPiece player = new PlayerPiece();
TilePiece tile = new TilePiece();
doShapes(player);
doShapes(tile);
}

public static void doShapes(GameShape shape) {
shape.displayShape();
}

}

class GameShape{
public void displayShape() {
System.out.println("Displaying Shape");
}
}

class PlayerPiece extends GameShape{
public void movePiece() {
System.out.println("Moving Game Piece");
}
}

class TilePiece extends GameShape{
public void getAdjacent() {
System.out.println("Getting Adjacent Tiles");
}
}

Polymorphism
package com.core;
public interface Animatable {
public void animate();
}
class PlayerPiece extends GameShape implements Animatable{
public void movePiece() {  System.out.println("Moving Game Piece");}
public void animate() { System.out.println("Animating... ");}
}
//Legal Declarations
PlayerPiece player = new PlayerPiece();
Object o = player;
GameShape shape = player;
Animatable mover = player;

package com.core;

public class TestAnimals {

public static void main(String [] args) {
Animal a = new Animal();
Animal b = new Horse();
c.buck(); // Can't invoke buck(); Animal class doesn't have that method
a.eat(); // Runs the Animal version of eat
b.eat(); // Runs the Horse version of eat
}
}
class Animal{
public void eat() {
System.out.println("Generic Animal Eating Generically");
}
}
class Horse extends Animal{
public void eat() {
System.out.println("Horse eating hay...");
}
public void buck() {}
}
 Rules of overiding
  • The argument list must exactly match that of the overridden method. if they don't match, you can end up with an overloaded method you didn't intend.
  • The return type must be he same as, or a subtype of, the return type declared in the original overridden method in the superclass.
  • The access level can't be more restrictive than that of the overridden method.
  • Instance methods can be overidden only if they are inherited in the subclass. A subclass within the same package as the instance's superclass can overridde  any superclass method that is not marked private or final. A subclass in different package can override only those non final methods marked public or protected (since protected methods are inherited by the subclass).
  • The overridding method can throw any unchecked (runtime) exception, regardless of whether the overridden methods declared the exception.
  • The overriding method must not throw checked exceptions that are new or broader than those declared by the overridden method. For ex, a method that declares a FileNotFoundException cannot be overridden by a method that declares a SQLException, Exception, or any other non-runtime exception unless it's a subclass of FileNotFoundException.
  • You cannot override a method marked final.
  • you cannot override a method marked static.
  • if a method can't be inherited, you cannot override it. Remember that overriding implies that you're implementing a method you inherited ! For ex. the belo code is not legal.

public class testAnimals{
    public static void main(String args[]){
        Horse h = new Horse();
       h.eat();
}
}
class Animal{
     private void eat(){
      syso("Generic animal eating");
}
}
class Horse extends Animal {}

Overloaded Methods
  • Overloaded methods let you reuse the same method name in a class, but with different arguments (and optionally, a different return type)
  • Overloaded method must change the argument list.
  • Overloaded methods can change the return type.
  • Overloaded methods can change the access modifier.
  • overloaded methods can declare new or broader checked exceptions.
 Strings are immutable Objects
 Once you have assigned









Comments
* The email will not be published on the website.
I BUILT MY SITE FOR FREE USING