Different class types in java

Chapter 8. Classes

A class declaration defines a new class and describes how it is implemented (§8.1).

A top level class (§7.6) is a class declared directly in a compilation unit.

A nested class is any class whose declaration occurs within the body of another class or interface declaration. A nested class may be a member class (§8.5, §9.5), a local class (§14.3), or an anonymous class (§15.9.5).

Some kinds of nested class are an inner class (§8.1.3), which is a class that can refer to enclosing class instances, local variables, and type variables.

An enum class (§8.9) is a class declared with abbreviated syntax that defines a small set of named class instances.

A record class (§8.10) is a class declared with abbreviated syntax that defines a simple aggregate of values.

This chapter discusses the common semantics of all classes. Details that are specific to particular kinds of classes are discussed in the sections dedicated to these constructs.

A class may be declared public (§8.1.1) so it can be referred to from code in any package of its module and potentially from code in other modules.

A class may be declared abstract (§8.1.1.1), and must be declared abstract if it is incompletely implemented; such a class cannot be instantiated, but can be extended by subclasses. The degree to which a class can be extended can be controlled explicitly (§8.1.1.2): it may be declared sealed to limit its subclasses, or it may be declared final to ensure no subclasses. Each class except Object is an extension of (that is, a subclass of) a single existing class (§8.1.4) and may implement interfaces (§8.1.5).

A class may be generic (§8.1.2), that is, its declaration may introduce type variables whose bindings differ among different instances of the class.

Class declarations may be decorated with annotations (§9.7) just like any other kind of declaration.

The body of a class declares members (fields, methods, classes, and interfaces), instance and static initializers, and constructors (§8.1.7). The scope (§6.3) of a member (§8.2) is the entire body of the declaration of the class to which the member belongs. Field, method, member class, member interface, and constructor declarations may include the access modifiers public , protected , or private (§6.6). The members of a class include both declared and inherited members (§8.2). Newly declared fields can hide fields declared in a superclass or superinterface. Newly declared member classes and member interfaces can hide member classes and member interfaces declared in a superclass or superinterface. Newly declared methods can hide, implement, or override methods declared in a superclass or superinterface.

Field declarations (§8.3) describe class variables, which are incarnated once, and instance variables, which are freshly incarnated for each instance of the class. A field may be declared final (§8.3.1.2), in which case it can be assigned to only once. Any field declaration may include an initializer.

Member class declarations (§8.5) describe nested classes that are members of the surrounding class. Member classes may be static , in which case they have no access to the instance variables of the surrounding class; or they may be inner classes.

Member interface declarations (§8.5) describe nested interfaces that are members of the surrounding class.

Method declarations (§8.4) describe code that may be invoked by method invocation expressions (§15.12). A class method is invoked relative to the class; an instance method is invoked with respect to some particular object that is an instance of a class. A method whose declaration does not indicate how it is implemented must be declared abstract . A method may be declared final (§8.4.3.3), in which case it cannot be hidden or overridden. A method may be implemented by platform-dependent native code (§8.4.3.4). A synchronized method (§8.4.3.6) automatically locks an object before executing its body and automatically unlocks the object on return, as if by use of a synchronized statement (§14.19), thus allowing its activities to be synchronized with those of other threads (§17 (Threads and Locks)).

Method names may be overloaded (§8.4.9).

Instance initializers (§8.6) are blocks of executable code that may be used to help initialize an instance when it is created (§15.9).

Static initializers (§8.7) are blocks of executable code that may be used to help initialize a class.

Constructors (§8.8) are similar to methods, but cannot be invoked directly by a method call; they are used to initialize new class instances. Like methods, they may be overloaded (§8.8.8).

Источник

What are the different types of classes in Java?

Any normal class which does not have any abstract method or a class that has an implementation of all the methods of its parent class or interface and its own methods is a concrete class.

Example

public class Concrete < // Concrete Class static int product(int a, int b) < return a * b; >public static void main(String args[]) < int p = product(2, 3); System.out.println("Product: " + p); >>

Output

Abstract class

A class declared with abstract keyword and have zero or more abstract methods are known as abstract class. The abstract classes are incomplete classes, therefore, to use we strictly need to extend the abstract classes to a concrete class.

Example

abstract class Animal < //abstract parent class public abstract void sound(); //abstract method >public class Dog extends Animal < //Dog class extends Animal class public void sound() < System.out.println("Woof"); >public static void main(String args[]) < Animal a = new Dog(); a.sound(); >>

Output

Final class

A class declared with the final keyword is a final class and it cannot be extended by another class, for example, java.lang.System class.

Example

final class BaseClass < void Display() < System.out.print("This is Display() method of BaseClass."); >> class DerivedClass extends BaseClass < //Compile-time error - can't inherit final class void Display() < System.out.print("This is Display() method of DerivedClass."); >> public class FinalClassDemo < public static void main(String[] arg) < DerivedClass d = new DerivedClass(); d.Display(); >>

In the above example, DerivedClass extends BaseClass(final), we can’t extend a final class, so compiler will throw an error. The above program doesn’t execute.

Output

cannot inherit from final BaseClass Compile-time error - can't inherit final class

POJO class

A class that contains only private variables and setter and getter methods to use those variables is called POJO (Plain Old Java Object) class. It is a fully encapsulated class.

Example

class POJO < private int value=100; public int getValue() < return value; >public void setValue(int value) < this.value = value; >> public class Test < public static void main(String args[])< POJO p = new POJO(); System.out.println(p.getValue()); >>

Output

Static class

Static classes are nested classes means a class declared within another class as a static member is called a static class.

Example

import java.util.Scanner; class staticclasses < static int s; // static variable static void met(int a, int b) < // static method System.out.println("static method to calculate sum"); s = a + b; System.out.println(a + "+" + b); // print two numbers >static class MyNestedClass < // static class static < // static block System.out.println("static block inside a static class"); >public void disp() < int c, d; Scanner sc = new Scanner(System.in); System.out.println("Enter two values"); c = sc.nextInt(); d = sc.nextInt(); met(c, d); // calling static method System.out.println("Sum of two numbers-" + s); // print the result in static variable >> > public class Test < public static void main(String args[]) < staticclasses.MyNestedClass mnc = new staticclasses.MyNestedClass(); // object for static class mnc.disp(); // accessing methods inside a static class >>

Output

static block inside a static class Enter two values 10 20 static method to calculate sum 10+20 Sum of two numbers-30

Inner Class

A class declared within another class or method is called an inner class.

Example

public class OuterClass < public static void main(String[] args) < System.out.println("Outer"); >class InnerClass < public void inner_print() < System.out.println("Inner"); >> >

Источник

Different Types of Classes in Java with Examples

A class is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include these components, in order:

  • Modifiers: A class can be public or has default access
  • class keyword: class keyword is used to create a class.
  • Class name: The name should begin with an initial letter (capitalized by convention).
  • Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
  • Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
  • Body: The class body surrounded by braces, < >.

We can define class members and functions inside the class. Different types of classes:

1. Static Class

We can declare a class as static if and only if it is a nested class. We can declare an inner class with the static modifier, such types of inner classes are called static nested classes. In the case of normal or regular class without the existing outer class object, there is no chance of existing inner class object i.e inner class object is strongly associated with an outer class object. In the case of static nested classes, there may be a chance of declaring the nested class object without an outer class object.

If we want to declare a nested class object from outside of the outer class then we can create as follows

Syntax:

Outerclassname.innerclassname objectvariable= new Outerclassname.innerclass name();

Example:

Test.Nested n= new Test.Nested();

Hence it is said that there may be a chance of existing nested class objects without existing outer class objects. In normal or regular inner classes we can’t declare any static members, but in static nested classes we can declare static members including the main method, so, we can invoke static nested class directly from the command prompt.

Properties of the static class:

  1. static class objects cannot be created.
  2. static class can only have static members.
  3. static class cannot access members (non-static) of the outer class.

Java

2. Final Class

The class can be declared as final by using the keyword ‘final’. If a class is declared as final we can’t extend the functionality of that class i.e we can’t create a child class for that class, i.e inheritance is not possible for final classes. Every method present inside the ultimate class is usually final by default, but every variable present inside the ultimate class needn’t be final. If we create the final class we cannot achieve inheritance. If we create a final method we cannot achieve polymorphism, but we can obtain security no one can change our code’s unique implementation, but the main disadvantage of the final keyword is we are missing the benefits of inheritance and polymorphism.

Note: If there is no specific requirement it is not recommended to use the final keyword.

Java

3. Abstract Class

We can declare a class as abstract by using the keyword ‘abstract’. For any java class if we are not allowed to create an object such type of class, we should declare with the abstract modifier, i.e for abstract classes instantiation is not possible. If a class contains at least one abstract method then compulsory we should declare a class as abstract otherwise we get a compilation error. Reason: If a class contains at least one abstract method then implementation is not complete and hence it is not recommended to create an object. If we need to restrict object instantiation compulsory we need to declare the class as abstract. A class containing either zero or more abstract methods can be an abstract class if we don’t want any instantiation. Example: HttpServletClass is abstract but it doesn’t contain any abstract methods.

If we are extending abstract class then for every and each abstract method of parent class we should always provide implementation, otherwise, we’ve to declare child class as abstract. In this case, the next-level child class is responsible to provide the implementation.

Источник

Читайте также:  Два фоновых изображения
Оцените статью