0% found this document useful (0 votes)
16 views6 pages

Static Imports - Abstract Class - Interface - Is

The document discusses Java features such as static imports, abstract classes, and interfaces. It outlines the advantages and disadvantages of static imports, explains the rules and usage of the abstract keyword, and describes the purpose and syntax of Java interfaces, including their role in achieving abstraction and multiple inheritance. Additionally, it provides sample code and a self-paced activity for practical application of these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Static Imports - Abstract Class - Interface - Is

The document discusses Java features such as static imports, abstract classes, and interfaces. It outlines the advantages and disadvantages of static imports, explains the rules and usage of the abstract keyword, and describes the purpose and syntax of Java interfaces, including their role in achieving abstraction and multiple inheritance. Additionally, it provides sample code and a self-paced activity for practical application of these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

OTHER CLASS FEATURES

The static imports


➢ This feature was introduced in Java SE 5.0.
➢ It facilitates the java programmer to access any static member of a class directly.
➢ There is no need to qualify it by the class name.
Syntax

import static <pkg_list>.<class_name>.<member_name>;


or
import static <pkg_list>.<class_name>.*;

Sample Program:

Advantage of static import


➢ Less coding is required if you have to access often any static member of a class.

Disadvantage of static import


➢ Overusing the static import feature will make the program unreadable and
unmaintainable.
The abstract keyword
➢ Abstract means “there are no concrete implementations”; therefore, abstract
methods do not have any implementation in their method body, just the method
signature.
➢ Abstract classes serve as a template that forces the subclasses to override methods
that should be present in all sub-classes.
Example:

Note:
1. if class/method is abstract, it should be written in italics in UML Diagram

Rules of abstract keyword


Do's
➢ An abstract keyword can only be used with class and method.
➢ An abstract class can contain constructors and static methods.
➢ An abstract class can contain the main method and the final method.
➢ If a class contains an abstract method, the class must be declared as abstract
➢ An abstract class can contain overloaded abstract methods.
➢ We can declare the abstract method with a throw clause.
Don'ts
➢ An abstract keyword cannot be used with variables and constructors.
➢ If a class is abstract, it cannot be instantiated.
➢ If a method is abstract, it doesn't contain the body.
➢ We cannot use the abstract keyword with the final keyword.
➢ We cannot declare abstract methods as private.
➢ We cannot declare abstract methods as static.

Sample code: AbstractShape

Java Interfaces
➢ An interface is a completely "abstract class" that is used to group related methods with
empty bodies.
➢ A Java Interface is a contract between the client code and the class that implements
that interface.

Notes on Interfaces
➢ Like abstract classes, interfaces cannot be used to create objects.
➢ Interface methods do not have a body - the body is provided by the implementing
class.
➢ To access the interface methods, the interface must be "implemented" (kind of
inherited) by another class with the implements keyword (instead of extends).
➢ Interface methods are by default abstract and public.
➢ All methods in the interface should be overridden by the implementing class.
➢ Interface attributes are by default public, static and final.
➢ An interface cannot contain a constructor (as it cannot be used to create objects)
➢ Java Interface also represents the IS-A relationship.
➢ Implementing multiple interfaces is Java’s way of implementing multiple
inheritance.

Why And When To Use Interfaces?


➢ To achieve security - hide certain details and only show the important details of an
object (interface).
➢ It is used to achieve abstraction.
➢ To implement multiple inheritance
Syntax:
interface <interface_name>
{
// declare constant fields
// declare methods that are abstract
// by default.
}
Relationship between classes and interfaces
➢ As shown in the figure given below, a class extends another class, an interface
extends another interface, but a class implements an interface.

Java Interface Example

Note: an interface is written in italics


A broken line represents an implementation
Sample code: InterfaceAccount
Implementing Multiple Interfaces
interface A
{
// members of A
}
interface B
{
// members of B
}
class C implements A, B
{
// abstract members of A
// abstract members of B
}

Multiple inheritance in Java by interface


➢ If a class implements multiple interfaces, or an interface extends multiple interfaces,
it is known as multiple inheritance

Interface inheritance
➢ it is possible for a java interface to inherit from another java interface, just like
classes can inherit from other classes. You specify inheritance using the extends
keyword.
Sample codes: InterfaceMultiple , InterfaceInheritance, InterfaceShape

Self-Paced Activity
A. Formative MP
1. Create a Java program that will accept numeric inputs. Based on the number of inputs, the
program will decide what shape it is. (1 input – circle, 2 inputs – square or rectangle, 3 inputs
– triangle). The program will then display the shape type, details, perimeter, and area.
Validate your inputs. Use the OOP approach(please refer to the given UML Diagrams)
Filename: InterfaceShape

You might also like