1. Inheritance
public class Animal{
}
public class Mammal extends Animal{
}
public class Dog extends Mammal{ //Dog IS-A Animal as well
}
instanceof shows if object belongs to class or not
The implements keyword is used by classes by inherit from interfaces. Interfaces can never be extended by the classes.
public interface Animal {}
public class Mammal implements Animal{
}
public class Dog extends Mammal{
}
Java only supports only single inheritance. This means that a class cannot extend more than one class. However, a class can implement one or more interfaces. This has made Java get rid of the impossibility of multiple inheritance.
Therefore following is illegal: public class extends Animal, Mammal{}
2.Overriding
overriding means to override the functionality of an existing method (by a sub class provided it is not final)
class Animal{
public void move(){
System.out.println("Animals can move");
}
}
class Dog extends Animal{
public void move(){
System.out.println("Dogs can walk and run");
}
}
Many rules exist for overriding a method by sub class
When invoking a superclass version of an overridden method the super keyword is used.
public void move(){
super.move(); // invokes the super class method
System.out.println("Dogs can walk and run");
}
3.Polymorphism
The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.Once declared,
public interface Vegetarian{}
public class Animal{}
public class Deer extends Animal implements Vegetarian{}
Now, the Deer class is considered to be polymorphic since this has multiple inheritance.
Deer d = new Deer();
Animal a = d;
Vegetarian v = d;
Object o = d;
All the reference variables d,a,v,o refer to the same Deer object in the heap.
An overridden method is invoked at run time, no matter what data type the reference is that was used in the source code at compile time.This behavior is referred to as virtual method invocation and the methods are referred to as virtual methods.
4.Abstraction
You just cannot create an instance of the abstract class. The class does not have much use unless it is subclass. Use the abstract keyword to declare a class abstract. The keyword appears in the class declaration somewhere before the class keyword.
public abstract class Employee
We can extend Employee class in normal way as follows:
public class Salary extends Employee
The abstract keyword is also used to declare a method as abstract (the class must be abstract too). An abstract method consists of a method signature, but no method body. This is done if you want the actual implementation of that method to be determined by child classes
public abstract class Employee
{
public abstract double computePay();
}
A child class that inherits an abstract method must override it. If they do not, they must be abstract and any of their children must override it.
5.Encapsulation
Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code.
public class EncapTest{
private int age;
public int getAge(){
return age;
}
public void setAge( int newAge){
age = newAge;
}
}
These methods are called getters and setters
6.Interfaces
An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.A class uses the implements keyword to implement an interface. An interface is implicitly abstract and so are the methods in it.
public interface NameOfInterface
{
//Any number of final, static fields
//Any number of abstract method declarations\
}
Many rules exist in implementing interface
The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface. Interfaces are not classes, however, and an interface can extend more than one parent interface.
public interface Hockey extends Sports, Event
public interface Sports
{
public void setHomeTeam(String name);
public void setVisitingTeam(String name);
}
public interface Football extends Sports
{
public void homeTeamScored(int points);
public void visitingTeamScored(int points);
}
An interface with no methods in it is referred to as a tagging interface and is used to create a common parent and add a data type in a class
7.packages
A Package can be defined as a grouping of related types(classes, interfaces, enumerations and annotations ) providing access protection and name space management. Use lower case for naming convention
package animals;
interface Animal {
public void eat();
public void travel();
}
If a class wants to use another class in the same package, the package name does not need to be used. Classes in the same package find each other without any special syntax.
An import statement is used to import classes or methods from other packages or you can use The fully qualified name of the class to refer to it
import payroll.*;
payroll.Employee
The name of the package must match the directory structure where the corresponding bytecode resides. In general, a company uses its reversed Internet domain name for its package names. Each component of the package name corresponds to a subdirectory.However, the path to the .class files does not have to be the same as the path to the .java source files. You can arrange your source and class directories separately, as:
<path-one>\sources\com\apple\computers\Dell.java
<path-two>\classes\com\apple\computers\Dell.class
The full path to the classes directory, <path-two>\classes, is called the class path, and is set with the CLASSPATH system variable.
public class Animal{
}
public class Mammal extends Animal{
}
public class Dog extends Mammal{ //Dog IS-A Animal as well
}
instanceof shows if object belongs to class or not
The implements keyword is used by classes by inherit from interfaces. Interfaces can never be extended by the classes.
public interface Animal {}
public class Mammal implements Animal{
}
public class Dog extends Mammal{
}
Java only supports only single inheritance. This means that a class cannot extend more than one class. However, a class can implement one or more interfaces. This has made Java get rid of the impossibility of multiple inheritance.
Therefore following is illegal: public class extends Animal, Mammal{}
2.Overriding
overriding means to override the functionality of an existing method (by a sub class provided it is not final)
class Animal{
public void move(){
System.out.println("Animals can move");
}
}
class Dog extends Animal{
public void move(){
System.out.println("Dogs can walk and run");
}
}
Many rules exist for overriding a method by sub class
When invoking a superclass version of an overridden method the super keyword is used.
public void move(){
super.move(); // invokes the super class method
System.out.println("Dogs can walk and run");
}
3.Polymorphism
The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.Once declared,
public interface Vegetarian{}
public class Animal{}
public class Deer extends Animal implements Vegetarian{}
Now, the Deer class is considered to be polymorphic since this has multiple inheritance.
Deer d = new Deer();
Animal a = d;
Vegetarian v = d;
Object o = d;
All the reference variables d,a,v,o refer to the same Deer object in the heap.
An overridden method is invoked at run time, no matter what data type the reference is that was used in the source code at compile time.This behavior is referred to as virtual method invocation and the methods are referred to as virtual methods.
4.Abstraction
You just cannot create an instance of the abstract class. The class does not have much use unless it is subclass. Use the abstract keyword to declare a class abstract. The keyword appears in the class declaration somewhere before the class keyword.
public abstract class Employee
We can extend Employee class in normal way as follows:
public class Salary extends Employee
The abstract keyword is also used to declare a method as abstract (the class must be abstract too). An abstract method consists of a method signature, but no method body. This is done if you want the actual implementation of that method to be determined by child classes
public abstract class Employee
{
public abstract double computePay();
}
A child class that inherits an abstract method must override it. If they do not, they must be abstract and any of their children must override it.
5.Encapsulation
Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code.
public class EncapTest{
private int age;
public int getAge(){
return age;
}
public void setAge( int newAge){
age = newAge;
}
}
These methods are called getters and setters
6.Interfaces
An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.A class uses the implements keyword to implement an interface. An interface is implicitly abstract and so are the methods in it.
public interface NameOfInterface
{
//Any number of final, static fields
//Any number of abstract method declarations\
}
Many rules exist in implementing interface
The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface. Interfaces are not classes, however, and an interface can extend more than one parent interface.
public interface Hockey extends Sports, Event
public interface Sports
{
public void setHomeTeam(String name);
public void setVisitingTeam(String name);
}
public interface Football extends Sports
{
public void homeTeamScored(int points);
public void visitingTeamScored(int points);
}
An interface with no methods in it is referred to as a tagging interface and is used to create a common parent and add a data type in a class
7.packages
A Package can be defined as a grouping of related types(classes, interfaces, enumerations and annotations ) providing access protection and name space management. Use lower case for naming convention
package animals;
interface Animal {
public void eat();
public void travel();
}
If a class wants to use another class in the same package, the package name does not need to be used. Classes in the same package find each other without any special syntax.
An import statement is used to import classes or methods from other packages or you can use The fully qualified name of the class to refer to it
import payroll.*;
payroll.Employee
The name of the package must match the directory structure where the corresponding bytecode resides. In general, a company uses its reversed Internet domain name for its package names. Each component of the package name corresponds to a subdirectory.However, the path to the .class files does not have to be the same as the path to the .java source files. You can arrange your source and class directories separately, as:
<path-one>\sources\com\apple\computers\Dell.java
<path-two>\classes\com\apple\computers\Dell.class
The full path to the classes directory, <path-two>\classes, is called the class path, and is set with the CLASSPATH system variable.
No comments:
Post a Comment