Class and Interface

Class :-  Object always created by looking towards class , class is considered as model template or blue print for an object.

There are 3 types of classes

  1. Concrete Class :- A class from which we can create object such class is called as “Concrete Class”.
  2. Empty Class :- A class which do not contain any data member and member function such type of class is called as “Empty Class”.

Example of Empty class

package emptyclass_example;

public class Test {

}

public class EmptyClass_Example {

public static void main(String[] args) {

Test testIntance = new Test();

}

}

Empty class is Concrete class.

Interview Question – What is size of object of empty class? and why?

Abstract Methods and Classes

  1. Abstract Class :- A class from which we cannot create object such type of class is called as “Abstract Class”.

Class Members

  • An abstract class may have static fields and static methods. You can use these static members with a class reference (for example, AbstractClass.staticMethod()) as you would with any other class.
  • An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
  • An Abstract Method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
  • Example of Abstract Method
abstract void moveTo(double deltaX, double deltaY);

If a class includes abstract methods, then the class itself must be declared abstract, as in:

public abstract class GraphicObject {

// declare fields

// declare nonabstract methods

abstract void draw();

}

When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.

Example of Abstract Class

abstract class GraphicObject {

int x, y;

...

void moveTo(int newX, int newY) {

...

}

abstract void draw();

abstract void resize();

}

Each nonabstract subclass of GraphicObject, such as Circle and Rectangle, must provide implementations for the draw and resize methods:

class Circle extends GraphicObject {

void draw() {

...

}

void resize() {

...

}

}

class Rectangle extends GraphicObject {

void draw() {

...

}

void resize() {

...

}

}

When an Abstract Class Implements an Interface

In the section on Interfaces, it was noted that a class that implements an interface must implement all of the interface’s methods. It is possible, however, to define a class that does not implement all of the interface’s methods, provided that the class is declared to be abstract. For example,

abstract class X implements Y {

// implements all but one method of Y

}

class XX extends X {

// implements the remaining method in Y

}

In this case, class X must be abstract because it does not fully implement Y, but class XX does, in fact, implement Y.

Static Keyword In Java

static members do not belongs to the object they belongs to the class.

  1. Static Field

-static field do not contribute to size of object.

-The data that is common for all objects of the class should be declared as static.

  1. Static Method

-Do not receive implicit “this” pointer/reference.

-Can not access non static member of class directly.

-This methods are written to modify/manipulate static fields in the class.

-They are used to create object of class.

Example

Calender cal1 = Calender.getInstance(); //here getIntance is static method

-Creating object using such static method in the class called as “Static Factory Pattern”

  1. Static Block

-When class is loaded first time into JVM class loader invokes static blocks declared in the class.

-Static block will be invoked in the order of their declaration in the class.

-It is used to perform one time initialization of the class.

 Final Keyword In Java

1.final local varible

-Local variable can be declared as final.

-The final variables can be initialized only ones(may or may not be at the point of declaration)

-This variable cannot be modified after once intialized.

Example

Void fun1(){

final int num1 = 100;

/*

*/

}

Void fun2(){

String name;

Int age;

/*

*/

}

Class test2{

Final Person p1 = new Person();

P1= new Person(); // It will give error

}
  1. Final field

– Non static final field can be initialized at the point of declaration in the class or within constructor.

– Static final field can be initialized at the point of declaration in the class or within static block.

  1. Final method

If method is declared as final it cannot be over ridden in sub-class otherwise

compiler raise error.

-Conceptually static method cannot be declared as final method(static method cannot be overridden)

  1. Final Class

The final class cannot be inherited otherwise compiler raise error.

– All methods in final class are treated as final methods.

Eg. String class is the final class

           Final Class           Abstract Class
Final class cannot be inherited Abstract class can be inherited
Final class method cannot be over ridden Abstract class method must be over ridden

Interface in details

Interface – An interface is not a class but a set of requirements for the classes that want to conform to the interface.

Eg.

public interface Comparable

{

int compareTo(Object other);

}

-To declare that a class implements an interface,use the inplements keyword.

– Methods in an interface are automatically public and fields are always public       static final.

class Employee implements Comparable

Properties of Interfaces

  1. You can never use the new operator to instantiate an interface :
x = new Comparable (…);  //ERROR
  1. Even though you can’t construct interface objects, you can still

Declare interface variables.

Comparable x; //OK

3.Interface variable must refer to an object of a class that implements

the interface

x = new Employee(...);//OK provided Employee implements comparable

Marker Interface

If interface does not contain any method declaration(i.e. Empty Interface)

Such interfaces are called “Marker Interface”.

-They are used to indicate that the class is associated with certain functionality.

Serializable and Cloneable are marker interfaces.


Search

Subscribe to YouTube

Training on Selenium

Like us on facebook

Jobs

Jobs

Cooking with Amol