Popular Feed

JAVA Class and Types

I will be discussing here following topics:

1 what is class and declaration about class?

2 Types of classes in java

·         POJO Class

·         Static Class

·         Concrete Class

·         Abstract Class

·         Final Class

·         Inner Class

                1.    Nested Inner Class

                2.    Method Local Inner Class

                3.    Anonymous Inner class

                4.    Static nested class


  what is class and declaration about class?

A class is the blueprint from which individual objects are created. Class is a one which contains states (variables), data type(primitive and Non-primitive) and behaviour (methods).

Ex:

package com.niit.springrest;
public class Test {
	String name; 	// state (variables) and String Non-primitive Data Type
	int age;  // state  (variables) and int Primitive Data Type
	char sex;	// state (variables) it can takes only M or F  
	double height; 	//state (variables)
	void takeToPark() { //behavior Or method
		System.out.println("Take in a R-15");
	} 
	int spendMoney() { //behavior 	Or method
		int money=5000;  
		return money;
	}
}
Let’s start Types of classes

  Types of classes in java

·         POJO Class :-   (Plain Old Java Object)

A class which contains only private variables and setter, getter methods to use those variables is called POJO class.

 In below POJO Java classes have to implement the Serializable interface? What will happen if I do not implement Serializable?

First this is no longer a Plain Old Java Object, Because it has annotations.

But staying on your premise, The Serializable is required in POJOs if those are intended to be used in Distributed Systems and Systems which use caching and flushing into files and reading back.

Mostly JPA implementations do run in Distributed manner and Use caching, thus this POJO is required to implement Serializable.

Example:

public class Customer implements Serializable {

	private int Custid;
	/** Holds first Name of the customer*/
	private String firstName;
	/** Holds last Name of the customer */
	private String lastName;
	/** Holds Customer mobile number of customer*/
	private long MobileNo;
	public int getCustid() {
		return Custid;
	}
	public void setCustid(intcustid) {
		Custid = custid;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	} 
	public void setLastName(String lastName) {
		this.lastName = lastName;
	} 
	public long getMobileNo() {
		return MobileNo;
	} 
	public void setMobileNo(longmobileNo) {
		MobileNo = mobileNo;
	}
}

 Static Class:

 In Java, A static class can contain static members only. We cannot create an abject for a Static class.

Basically Static is a keyword and used to describe how object are managed within memory.

A static object belongs specifically to the class and instead of instances of that class. 

Example:-

package com.niit.springrest;
public class Test {
	private static String sc = "school";
	public static class HighSchool{
		public void displayOutput(){
			System.out.println(" HighSchool is: " + sc);
		}
	}
	public static void main(String[] args){
		//creating instance of static class School.
		HighSchool bs = new Test.HighSchool();
		//calling the method
		bs.displayOutput();
	}
}

OUTPUT:

HighSchool is: School

 ·         Concrete Class

Any normal class which doesn’t have any kind of abstract method but we can extend an abstract class than override abstract method. Concrete class is a class that has an implementation for all of its methods. They cannot have any unimplemented methods. 

Example :-

// Java program to illustrate concrete class

//This is an interface

interface Y{
	int product(int x, int y);
}
// This is an abstract class
abstract class Product implements Y{
	// this method calculates
	// product of two numbers
	public int product(int x, int y){
		return x * y;
	}
}
// This is a concrete class that implements

class Main extends Product{
	// main method
	public static void main(String args[]){
		Main ob = new Main();
		int p = ob.product(20, 10);
		// print product
		System.out.println("Production: " + p + "Product");
	}
}

final class Super{
	private int data =30;
}
public class Sub extends Super{
	public static void main(Stringargs[]){
	}
}

OutPUT:-  Production: 200 product

  ·         Final Class

If we declared class, variable or method as final, its value remains the same throughout.

The main purpose of using a class being declared as final is to prevent the class from being subclasses. If a class is marked as final then no class can inherit any feature from the final class.

 Example: 

final class Super{
	private int data =30;
}
public class Sub extends Super{// compile time error
	public static void main(Stringargs[]){
	} 
}

Inner class:-

If any class member of another class that class means Inner class.

In JAVA four type of inner classes.

1.    Nested Inner Class

2.    Method Local Inner Class

3.    Anonymous Inner class

4.    Static nested class

 1.   Nested Inner Class

 It can access any private instance variable of an outer class. Like any other instance variable, we can have  access modifier private, protected, public and default modifier.

package com.niit.springrest;
public class Test {
	// Simple nested inner class
	class InnerClass {
		public void show() {
			System.out.println("Inside a nested(InnerClass) class method ");
		}
	}
}
class Main {
	public static void main(String[] args) {
		Test.InnerClass in = new Test().new InnerClass();
		in.show();
	}
}

 Output:

Inside a nested(InnerClass) class method 

2.   Method Local Inner Classes 

If inner class declared within a method of an outer class, then its work as Method Local Inner Classes.

 class OuterClass {

	void outerClassMethod() {
		System.out.println("outerclassMethod");
		// Inner class is local to outerclassMethod()
		class InnerClass {
			void innerClassMethod() {
				System.out.println("innerclassMethod");
			}
		}
		InnerClass y = new InnerClass();
		y.innerClassMethod();
	}
}
 public class Test {
	public static void main(String[] args) {
		OuterClass x = new OuterClass();
		x.outerClassMethod();
	}
}

 Output:-

outerclassMethod

innerclassMethod

 3.    Anonymous Inner class

If Inner class are declared without any name, so this type of class work as Anonymous Inner class.

 It’s can be created two ways,

·         Subclass as a specified

·         Implement of the specified Interface

Subclass as a Specified 

class Bike {
	void service() {
		System.out.println("This is Service method of super class Bike");
	}
}
class Test { 
	//  An anonymous class with Bike as base class
	static Bike d = new Bike() {
		void service() {
			super.service();
			System.out.println("This is Discover bike class");
		}
	};

	public static void main(String[] args){
		d.service();
	}
}

Output:-

This is Service method of super class Bike

This is Discover bike class

In the above code, there are two class Bike and Discover. Here bike acts as a superclass and anonymous class acts as a subclass and both classes have a method show(). In the anonymous class show(), the method is overridden.

Implement of the specified Interface

class Test {
	// An anonymous class that implements Hello interface
	static Discover h = new Discover() {
		public void service() {
			System.out.println("This is an anonymous class");
		}
	}; 
	public static void main(String[] args) { //This is an anonymous class
		h.service();
	}
}
interface Discover {
	void service(); 
}  

OutPut:- 

This is an anonymous class 

4.   Static Nested Class    

     static inner class looks like a Nested class. It’s defining static member of the outer class.

class Test {
	private static void outerMethod() {
		System.out.println("inside outerMethod");

	}
	// A static inner class
	static class Innerclass {
		public static void main(String[] args) {
			System.out.println("inside inner class Method");
			outerMethod();

		}
	}
}

           Output:-  

inside inner class Method

inside outerMethod

No comments: