SCJP: Class Declarations #Java Short Notes #SCJP

class declarations

  • Start with modifiers such as public, private followed by class keyword
  • The class name, with the initial letter capitalized
  • The name of the class's parent (superclass), preceded by the keyword extends (if any). A class can only extend (subclass) one parent.
  • list of interfaces implemented by the class, preceded by the keyword implements (if any). A class can implement more than one interface
  • The class body, surrounded by braces, {}.
  • Class member variable declarations
    • Requires three components, in order:
    • Zero or more modifiers, such as public or private
    • The field's type
    • The field's name.

Abstract Classes

  • A class declared with abstract keyword is an abstract class. It may or may not include abstract methods
  • Abstract classes cannot be instantiated
  • Abstract classes can be subclassed
  • Class containing abstract methods, must be declared to be abstract
  • Abstract methods are methods declared without implementation (braces)
  • The subclass of an abstract class must provide implementations of all the abstract methods otherwise the subclass itself needs to be declared as abstract.
  • An abstract class may have static and final fields and methods. Interfaces can not
  • If an abstract class contains only abstract method declarations with no implementations, it should better be declared as an interface
  • It's a good design to declare a common abstract class with common methods with implementations for several very related classes (will extend the abstract class)
  • An abstract class can implement an interface but are not bound to implement all interface methods

Nested Classes

  • Classes declared under another class are called nested classes
  • Two types: Static Nested: declared with static keyword, Inner Nested: declared with no static keyword
  • inner classes have access to other members of the outer class including private members
  • Static nested classes do not have access to other members of the outer class
  • Static nested classes are accessed using the outer class name such as: OuterClass.StaticNestedClass
  • To use inner classes, the outer class must be instantiated first. Then, inner object can be created as follows: OuterClass.InnerClass innerObject = outerObject.new InnerClass();
  • Local inner classes: Declared within the body of a method
  • Anonymous inner classes: Declared within the body of a method without naming it
  • Inner classes may have similar access modifiers like other outer class members
  • Why use nested classes:
    • For logically grouping classes that are only used in one place
    • It increases encapsulation.
    • May lead to more readable and maintainable code

From: http://sitestree.com/?p=4866
Categories:Java Short Notes, SCJP
Tags:
Post Data:2012-09-13 21:25:27

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

Leave a Reply