Current Android Release version:
Version Code: Pie
Version: 9
API level: 28

Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Abstract class in Java

A class that is declared with "abstract" keyword is called as abstract class.
A method that is declared as abstract and it does not have implementation is known as abstract method.

abstract class A{} 
abstract void checkStatus(); //no implementation

Important points to remember:
  • abstract classes can not be instantiated but it can be inherited
  • abstract classes can have both abstract and non abstract methods
  • If any one single method is abstract in class then whole class must be declared as abstract

Understanding final keyword in Java

In Java, final keyword can be used with variable, method, class

final variable:
  • we can not change value of final variable once it is initialized
  • if final variable not initialized (blank final variable) at the time of declaration, it must be initialized in constructor
  • A static final variable that is not initialized during declaration can only be initialized in static block.
final method:
  • final methods can not be overridden but methods can be inherited
final class:
  • final class can not be inherited
Basic points to remember:
  • All variables declared in interface are by default final
  • constructor can not be declared final
  • If method parameters are declared final then the value of these parameters cannot be changed
  • Local final variable must be initialized during declaration

Understanding Static keyword in Java

Static keyword can be used with variable, methods, block and nested class.

Static variable (Class variables):
  • static variable belongs to the class and can be accessed without creating instance of the class
  • static variables are initialized only once at the start of execution. These variables will be initialized first before initialization of any instance variables
  • only single copy of static variable is created and shared among all the instances of the class
  • static variable gets memory only once in class area at the time of class loading
Static method (Class methods):
  • static method belongs to the class and can be accessed without creating instance of the class
  • static methods can not access non static data member or can not call non static method. It can access only static data
  • "this" and "Super" can not be used
Static block:
  • It is used to initialize static data member
           static int a, b;
           static {
                   a = 10;
                   b = 20;
          }
  • static block will be executed before main method at the time of class loading
         class sample {
              static {
                      System.out.println("This will be executed before main");
              }
              public static void main(String args[]) {
                     System.out.println("Main method");
              }
         }

Static nested class:
  • Java does not allow you to create top level static class, only nested static classes are allowed
  • nested classes that are declared with static are called static nested classes

Object Oriented Design Principles

To make the design clean, Every programmer must follow the five basic principles of object-oriented programming and design.

S.O.L.I.D is an acronym for the first five object-oriented design(OOD) principles by Robert C. Martin, popularly known as Uncle Bob.

S.O.L.I.D means:

  • S – Single-responsibility principle
  • O – Open-closed principle
  • L – Liskov substitution principle
  • I – Interface segregation principle
  • D – Dependency Inversion Principle

Single-responsibility principle: One class should have one and only one responsibility.
It means a class should have only one job.

Open-closed principle: Objects or entities should be open for extension and closed for modification.

Liskov substitution principle: Sub types must be substitutable for super type i.e. methods or functions which uses super class type must be able to work with object of sub class without any issue.

Interface segregation principle: Clients should not be forced to implement unnecessary methods which they will not use.

Dependency Inversion Principle: High level modules should not depend on low level modules. Both should depend on Abstractions.

How to capitalize the first letter of a String in Java?

String name= "app development"; 
Character.toUpperCase(name.charAt(0));  //App development

Eclipse Short cuts


File Navigation:

CTRL SHIFT R: Open a resource.
CTRL E: Open a file (editor) from within the list of all open files.
CTRL PAGE UP or PAGE DOWN:  Navigate to previous or next file from within the list of all open files.
ALT <- or ALT ->:  Go to previous or next edit positions from editor history list.
                              java editing – Eclipse Shortcuts
CTRL SPACE: Type assist
CTRL SHIFT F: Format code.
CTRL O:  List all methods of the class and again CTRL O lists including inherited methods.
CTRL SHIFT O: Organize imports.
CTRL SHIFT U: Find reference in file.
CTRL /: Comment a line.
F3: Go to the declaration of the variable.
F4: Show type hierarchy of on a class.
CTRL T: Show inheritance tree of current token.
SHIFT F2: Show Java doc for current element.
ALT SHIFT Z: Enclose block in try-catch.

General Editing:
F12: Focus on current editor.
CTRL L: Go to line number.
CTRL D: Delete a line.
CTRL <- or ->: Move one element left or right.
CTRL M: Maximize editor.
CTRL SHIFT P: Go to the matching parenthesis.
Debug, Run: Eclipse Shortcuts
CTRL . or , : Navigate to next or previous error.
F5: Step into.
F6: Step over.
F8: Resume
CTRL Q: Inspect.
CTRL F11: Run last run program.
CTRL 1: Quick fix code.

Search:
CTRL SHIFT G: Search for current cursor positioned word reference in work space
CTRL H: Java search in work space.