Posts

Showing posts with the label Part G : JAVA Concepts

Anonymous Classes in Java (Part 10)

Image
Pre-requisites:- Inner Classes , Interface , Method Overridding   Anonymous Classes It is an inner class without a name and for objects cannot be created. An anonymous inner class can be useful when making an instance of an object with certain extras such as overriding methods of a class or interface, without having to actually create a subclass for it. Anonymous inner classes are useful in writing implementation classes for listener interfaces in graphical user interface programming (GUI) . Syntax:- Class_name [obj name] = new Class_name() { //anonymous class (child class) //override methods of parent classes / interfaces / abstract classes }; Declaring an Anonymous Classes An Anonymous class in Java can be created by two ways: 1. By Classes (Abstract or Normal) Eg:- class Machine { public void start () { System.out.println(" Parent Machine Starting. "); } } public class MyClass { public stati...

Enums in Java (Part 9)

Image
Pre-requisites:-  Switch Statement ,  Enhanced-for Loop Java Enums An enum  is a special class that represents a group of  Constants . Enum is short for Enumeration  which means specifically listed. For example the 4 suits in a deck of playing cards may be 4 enums named Club, Diamond, Heart, and Spade, belonging to an enumerated type named Suit.   Declaring an Enum To create an enum  , use the enum  keyword, and separate the constants with a comma. Note that they should be in uppercase letters: Syntax:- enum Enum_Name { //elements(Constants) in UpperCase letters } 1. Enum Inside / Outside Class Enum can be declared inside or outside a class , the rules for declaring both of the is same. Whenever an enum element is to be accessed , state the enum name first then " . " and followed by element name.  Eg:- //declaring an enum enum Color { RED , YELLOW , BLUE , GREEN ; } public class M yClass { ...

Interfaces in Java (Part 8)

Image
Pre-requisites : Inheritance , Abstraction   Java Interface Another way to achieve abstraction  is through Interfaces. There are only abstract methods in Interfaces and none of them have method body. It is also used to achieve multi-inheritance. It provides Total Abstraction  (100% abstraction). Declaring an Interface : Rules for declaring   Interface: Like  abstract classes , interfaces  cannot  be used to create objects. Interface methods do not have a body. On implementation of an interface, you must override all of its methods. Interface methods are by default  abstract  and  public Interface attributes are by default  public ,  static  and  final An interface cannot contain a constructor. Syntax:- interface interface_name { //abstract methods only } Implementation Of Interface 1. Single-Level Inheritance A Class extends a Class , An Interface extends an Interface too , only A Class implements an...

Abstraction in Java (Part 7)

Image
Pre-requisites : Inheritance , Upcasting , Method Overridding   Java Abstraction Abstraction is the process of hiding implementation details and only showing essential information to the user. In other words only outer information is provided to the user while internal details is hidden.  Abstraction mainly focuses on what the object does rather than how it is done. There are two ways to achieve abstraction:     1. Abstract Classes (Provides 0 to 100% abstraction)     2. Interface (Provides 100% abstraction)   Abstract Classes A class which is declared with an abstract keyword is called as an Abstract Class. It can have both abstract methods and non-abstract methods. To access its methods it must be inherited . Abstract Classes cannot be instantiated i.e its objects cannot be created. Rules For Creating Abstract Classes : An abstract class must be declared with an abstract keyword. To access an Abstract Class , it must be inherited and forcefull...

Upcasting & Downcasting (Part 6)

Image
Pre-requisite : Inheritance  , Methods Upcasting & Downcasting Typecasting is one of the most important concepts which deals with the conversion of one data type to another datatype implicitly or explicitly. In this blog , the concept of the typecasting for objects is explained. Just like casting of datatypes ,  objects can also be typecasted. However, in objects, there are only two types of objects parent object and child object. There are two types of typecasting : 1. Up-Casting  Upcasting is typecasting   of a child object to a parent object .  Upcasting can be done implicitly .  Upcasting gives us the flexibility to access the parent class members but it is not possible to access all the child class members , overridden methods can be accessed. Note:- As we saw in Inheritance , A Parent Class can have many child classes i.e many child classes can inherit a parent class . It means that a child can inherit all the properties of a parent ( Up Casting...