- A Method is a set of code, which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name.
- A method has a signature consisting of its name and the number and types of its parameters (also called arguments or actual parameters). The parameters in the declaration of the method are its formal parameters.
- A method has a return type, which is not part of its signature. If the return type is other than void, then the method must return a value of the specified type.
- Every method must have a signature that is unique within its class. Methods in other classes (even super classes and subclasses) may have the same signature.
The two kinds of polymorphism are overloading and overriding.
- In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. In this case, the methods are overloaded, and the process is referred to as method overloading.
- Overriding occurs when a class declares a method with the same signature as that of an inherited method. When a message is sent to the object (or class, if it's a class method), the locally defined method is the one that is used. Overriding is commonly used to make methods more specific.
When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. The version of the method defined by the super class will be hidden.
e. Interface and abstract classes
Abstract classes: An abstract class is declared using
"abstract" keyword. It allows putting all common method names in one class without writing the actual implementation code. An abstract class may or may not have abstract methods. It cannot be instantiated, but can be extended into sub-classes.
Abstract method: An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon)
Interfaces: An interface is an reference type which is similar to a class. It can contain only constants, methods signatures (there are no method boundaries) and nested types. Interfaces cannot be instantiated. They can be implemented by classes or extended by other interfaces.
Difference between Abstract classes and Interfaces
- Abstract classes can contain fields that are not static and final and they can contain implemented methods, But Interfaces cannot.
- Variables declared in interface are by default final. An abstract class may contain non-final variables
- Members of interface are public by default. An abstract class can have private, protected or public members
- Java interface should be implemented using keyword "implements"; A Java abstract class should be extended using keyword "extends".
Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. Classes can be derived from other classes, thereby inheriting fields and methods from those classes.
A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a super class (also a base class or a parent class). When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write them yourself.
Types of inheritance:
- Simple Inheritance: When a subclass is derived simply from it's parent class then this mechanism is known as simple inheritance. In case of simple inheritance there is only a sub class and it's parent class. It is also called single inheritance or one level inheritance.
- Multilevel Inheritance: When a subclass is derived from a derived class then this mechanism is known as the multilevel inheritance. The derived class is called the subclass or child class for it's parent class and this parent class works as the child class for it's just above (parent) class. Multilevel inheritance can go up to any number of levels.
- Multiple Inheritance: The mechanism of inheriting the features of more than one base class into a single class is known as multiple inheritances. Java does not support multiple inheritances but the multiple inheritances can be achieved by using the interface.
g. Inner classes
An inner class is a class declared within another class. The four kinds of inner class are:
- Member class: A member class is defined at the top level of the class, along with fields and methods. It may have the same access modifiers as variables (public, protected, package, static, final), and is accessed in much the same way as variables of that class.
- Static member class: A static member class is defined like a member class, but with the keyword static. Despite its position inside another class, a static member class is actually an "outer" class--it has no special access to names in its containing class. To refer to the static inner class from a class outside the containing class, use the syntax OuterClassName.InnerClassName. A static member class may contain static fields and methods.
- Local inner class: A local inner class is defined within a method, and the usual scope rules apply to it. It is only accessible within that method; therefore access restrictions (public, protected, package) do not apply. However, because objects (and their methods) created from this class may persist after the method returns, a local inner class may not refer to parameters or non-final local variables of the method.
- Anonymous inner class: An anonymous inner class is one that is declared and used to create one object (typically as a parameter to a method), all within a single statement. The anonymous inner class may either extend a class or implement an interface
An Enums is a class with a fixed number of instances that are defined within the class itself. Enums are used where there is a small, unchanging, predefined set of values, such as the days of the week.
Advantages over using small integers:
- They provide better type safety
- Better readability
- They can be read in, printed out, and compared with == (as well as equals).
Every Enums extends and inherits from java.lang.Enum. All constructors declared within an Enums are automatically private. Since Enums are classes, they can have methods, as well as constructors with parameters.
Every Enums class creates a new namespace, so that the same name may be used as a value in different Enums classes.