Sunteți pe pagina 1din 3

Interface :

it is a reference type, similar to class, that can contain only constants, method signatures and nested types. No method bodies. cannot be instantiated; only be implemented by other classes or extended by other interfaces. When a class implements an interface, it provides a body for each of the methods declared in the interface. Interfaces provide an alternative to the lack of multiple inheritance in Java. A class can inherit from only one class but it can implement more than one interface. Therefore, objects can have multiple types: the type of their own class and the types of all the interfaces that they implement.

Defining an Interface :
interface declaration consists of <modifiers> <keyword interface> <interface name> <list of parent interfaces(if any) preceded by keyword extends> <interface body> eg public interface GroupedInterface extends Interface1, Interface2{ double PI = 3.14759; // constant declarations void abc(int x,int y); // method signatures } Like classes, interfaces can extend other interfaces, however, unlike classes, they can extend multiple interfaces. Interface methods are implicitly public and abstract, and hence the public modifier need not be mentioned. Constants declared are implicitly public, static and final. And hence can be omitted from mention.

Interface as a Type :
If a ref var of interface type is defined, any obj assigned to it must be an instance of a class that implements the interface. An object needs to be casted to the interface type, before which the method(eg:comparison) can be applied on it. Eg: Relatable obj1 = (Relatable) object1; Relatable obj2 = (Relatable) object2; if((obj1).isLargerThan(obj2)>0) return obj1; else return obj2;

SUMMARY :
Interface defines a protocol of commn btwn 2 obj. contains signatures, but no implementations, and might contain constant defns a class that implements an interface must implement all the methods declared in the interface. an interface name can be used as a type.

INHERITANCE :
A sub-class inherits the members(fields & methods) of a super class. Since constructors are not members, they are not inherited. However, they can be invoked by the sub-class. In the absence of any explicit super-class, every class is implicitly a sub-class of Object( package : java.lang). When defining the constructor of a sub-class, the additional field of the sub-class can be initialized and the rest of the fields inherited can be initialized by making a call to the constructor of the parent class by use of super(); ie: You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.

OverRiding & Hiding : OverRiding : 1. overriding of instance method. 2. displays the content of the sub-class. Sub-class method overrides that of the super-class Hiding : 1. overriding of class method(static method) 2. displays the content of the super-class. Super-class method hides the sub-class method. Defining a Method with the Same Signature as a Superclass's Method Superclass Instance Method Subclass Instance Method Subclass Static Method
Overrides compile-time error

Superclass Static Method


compile-time error Hides

Super keyword : If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super. Eg : super.printMethod(); Can be used to refer to a hidden field (although hiding fields is discouraged). Can be used to invoke super-class's constructor. It must be the 1st line of the sub-class constructor. Eg : super(startCadence, startSpeed, startGear); If the super-class constructor is not explicitly called by the sub-class, the compiler calls it implicitly. Equals( ) method : is an instance method of the class Object in the java.lang package. Returns true or false. Works correct for primitive data types. For comparing objects, the method needs to be overridden in accordance to the object. Eg :
public class Book { ... public boolean equals(Object obj) { if (obj instanceof Book) // equals() being overridden

else } }

return ISBN.equals((Book)obj.getISBN()); return false;

Consider this code that tests two instances of the Book class for equality:
// Swing Tutorial, 2nd edition Book firstBook = new Book("0201914670"); Book secondBook = new Book("0201914670"); if (firstBook.equals(secondBook)) { System.out.println("objects are equal"); } else { System.out.println("objects are not equal"); }

Other useful methods of Object class are : toString(), clone(), getClass() Final Classes & Methods : final methods cannot be overridden. Final class cannot be sub-classed/inherited. Abstract Classes & Methods : Abstract Class : Ones declared as abstract; not necessary to contain abstract methods. Can only be sub-classed and not instantiated. Application comes in case of inheritance. Abstract Method : Methods that are declared, but not implemented. Can be declared only within in abstract class. The sub-class provides the implementation of the abstract methods.

S-ar putea să vă placă și