Submit Your Site To The Web's Top 50 Search Engines for Free! Java Tutorial: Learn Java Basics For Free | Learn Java
Showing posts with label Oops Concept. Show all posts
Showing posts with label Oops Concept. Show all posts

Tuesday, June 13, 2023

A Basic Java Program For Beginners 2023

 A Basic Java Program For Beginners 2023

                 Static Nested Class


1) Static Nested ClassIn java static nested class is a class which is defined in a class with static keyword.

static nested class cannot access non-static data member and methods of outer class. It can access only static data member of outer class including private.

 Java Static Nested Class Example

class Simple
{
static int a = 700;
static class SimpleInner
{
void show()
{
System.out.println("value is"+a);
}
}
public static void main(String args[])
{
Simple.SimpleInner s = new Simple.SimpleInner();
s.show();
}
}

https://cjavapoint.blogspot.com/?m=1
Blogspot
Core Java Interview Questions - Learn Java
Java Tutorial or Learn Java or Core Java Tutorial or Java Programming Tutorials for beginners and professionals with core concepts and examples

Non-Static Nested Class(inner class)


*1) Member Inner Class* 
 
In java member inner class is a class which is defined in class but outside of a method is called member inner class.

*Java Member Inner Class Example* 

Java member inner class defined in a class but outside of method of that class.

In this example we will create outer class(MemberOuter) and inner class(MemberInner) and declare a private data member in outer class and access this private data member in inner class because inner class can access all the data members and methods in inner class, including private data member and methods.

class MemberOuter
{
private int salary = 4000;//private data member of outer class
class MemberInner
{
void get()
{
System.out.println("salary is "+salary);//access in inner class
}
}
public static void main(String args[])
{
MemberOuter mo = new MemberOuter();
MemberOuter.MemberInner mi = mo.new MemberInner();
mi.get();
}

output : salary is 4000 

Note :

(1)  In case of java outer and inner class, Here the java compiler creates two .class files the first is MemberOuter.class and MemberOuter$MemberInner.class.


(2) If you want to instantiate inner class, you must have to create the instance of outer class and instance of inner class is created inside the instance of outer class.

*2) Anonymous Inner Class* 

In java anonymous inner class is a class that have no name is called anonymous inner class. This class can be instantiated only once. It is usually declared inside a method or block.


Anonymous inner class can be created by two ways in java 

Using class(abstract or concrete)
Using interface


*Java Anonymous Inner Class Example* 

 abstract class AIExample
{
abstract void show();
}
class Test
{
public static void main(String args[])
{
AIExample a = new AIExample()
{
void show()
{
System.out.println("Method of anonymous class");
}
};
a.show();
}
}


output : Method of anonymous class 
 
The above example is performing anonymous by using abstract class.

*Java Anonymous Inner Class Example - Using Interface* 


This is simple example of anonymous inner class using interface.
 
interface AnonymousExmple
{
void show();
}
 
class Test
{
public static void main(String args[])
{
AnonymousExmple ae = new AnonymousExmple()
{
public void show()
{
System.out.println("hello java ");
}
};
ae.show();
}
}
output : hello java

*3) Java Local Inner Class* 

In java local inner class is a class which is created inside a method is called local inner class in java. If you want to invokes the method of local inner class, you must instantiate this class inside the method.


*Java Local Inner Class Example* 

class LICExample
{
private int a = 40;
void show()
{
class LICExample1
{
void get()
{
System.out.println("value is "+a);
}
}
LICExample1 l = new LICExample1();
l.get();
}
public static void main(String args[])
{
LICExample ll = new LICExample();
ll.show();
}
}

output : value is 40

Sunday, July 24, 2022

What are Major Features of java New Update Varsion 2022 ?

         Q: What are the major features of Java ?

             Answer:
             A list of most important features of Java language is given below

1. Simple

Java is easy to learn and its syntax is quite simple, clean and easy to understand. The confusing and ambiguous concepts of C++ are either left out in Java or they have been re-implemented in a cleaner way.
Eg : Pointers and Operator Overloading are not there in java but were an important part of C++.

2. Object Oriented

Java is an object-oriented programming language. Everything in Java is an object. Object-oriented means we organize our software as a combination of different types of objects that incorporates both data and behavior. Object-oriented programming (OOPs) is a methodology that simplifies software development and maintenance by providing some rules.

3. Robust

Robust simply means strong. Java is designed to eliminate certain types of programming errors. Java is strongly typed, which allows extensive compile-time error checking. It does not support memory pointers, which eliminates the possibility of overwriting memory and corrupting data. In addition, its automatic memory management (garbage collection) eliminates memory leaks and other problems associated with dynamic memory allocation/de-allocation.

4. Platform Independent

Unlike other programming languages such as C, C++ etc which are compiled into platform specific machines. Java is guaranteed to be write-once, run-anywhere language.On compilation Java program is compiled into bytecode. This bytecode is platform independent and can be run on any machine, plus this bytecode format also provide security. Any machine with Java Runtime Environment can run Java Programs.Java is platform Independent Language

5. Secure

When it comes to security, Java is always the first choice. With java secure features it enable us to develop virus free, temper free system. Java program always runs in Java runtime environment with almost null interaction with system OS, hence it is more secure. Java is designed to be secure in a networked environment too. The Java runtime environment uses a bytecode verification process to ensure that code loaded over the network does not violate Java security constraints.

6. Multi Threading

Java multithreading feature makes it possible to write program that can do many tasks simultaneously. Benefit of multithreading is that it utilizes same memory and other resources to execute multiple threads at the same time, like While typing, grammatical errors are checked along.

7. Architectural Neutral

Compiler generates bytecodes, which have nothing to do with a particular computer architecture, hence a Java program is easy to intrepret on any machine.

Monday, December 16, 2019

Aggregation in Java: Definition and Examples -Learn Java

Aggregation in Java: Definition and Examples -Learn Java

Without existing container object if there is a chance of existing contained objects
such type of relationship is called aggregation .
in aggregation objects have weak association.

Example:

Within a department there may be a chance of several professors will work whenever 
we are closing department still there may be a chance of existing department object the
 relationship between department and professor is called aggregation where the objects
 having weak association.

Example:

https://cjavapoint.blogspot.com/

Saturday, December 14, 2019

Composition in Java Example - Learn Java

Composition in Java Example - Learn Java

without existing container object if there is no chance of existing contained object
then the relation between container object and contained object is called composition 
which is a strong association.

Example:

Any university consists of several departments whenever university object destroys automatically 
all the department object will be destroys that is without existing dependent object hence there are associated and this relationship is called composition .



Learn Java online free and learn java programming this is the
best way to learn java. this is best java tutorial.in this tutorial          
all the programs start with hello java,hello world.Enterprise applications         
programming language

HAS-A relationship in Java - Learn java

HAS-A relationship in Java - Learn java

1.HAS-A Relationship is also knows as composition(or) aggregation.
2.There is no specific keyword to implement HAS-A relationship but mostly 
    we can use new operator.
3.The main advantage of  reusability.

Example:-

class Engine
{
 // engine functionality
}
class Car
{
   Engine e=new Engine();
  //------------------------------;
  //------------------------------;
  //------------------------------;
}

NOTE:-

class car has-a engine reference.
The main dis-advantage of has-a relationship increases dependency between the
components and creates maintains problems.


Learn Java online free and learn java programming this is the
best way to learn java. this is best java tutorial.in this tutorial          
all the programs start with hello java,hello world.Enterprise applications         
programming language

Thursday, October 3, 2019

What is “tightly encapsulation” in Java? - Learn Java

What is “tightly encapsulation” in Java? - Learn Java

Encapsulation In Java With Realtime Example

A class is said to be tightly encapsulated
If and only if each and every variable of class declared as private.

And there is  no need to be worry about whether class contains corresponding 
getter and setter methods or not or whether these methods are declare as public 
or not.

Encapsulation In Java With Realtime Example:

Example:1
public class Account
  {
           private double balance;
           public double getBalance()
          {
                return balance;
          }
  }

Which of the following classes are tightly encapsulated

 public class A
 {
    private int x=11;(Valid)
 }
class B extends A
 {
    int y=20;(Invalid)
 }
 class C extends A
{
private int z=40;(Valid)
 }

Note : If the parent class is not tightly  encapsulated then  
child class will  not be tightly encapsulated.

learn java online free and learn java programming this is the
best way to learn java. this is best java tutorial.in this tutorial          
all the programs start with hello java,hello world.Enterprise applications         
programming language

Saturday, September 28, 2019

Encapsulation in Java - Learn Java

Encapsulation in Java - Learn Java

Encapsulation Real Time Example

Binding a data and corresponding method in a single unit is called encapsulation.
If any class follows data hiding and abstraction such type of class is called encapsulated class.


ENCAPSULATION= ABSTRACTION+DATA HIDING

The Main Advantage of Encapsulation.

  1. we can achieve security.
      2. it is improves maintainability and modularity of application.

The Main Disadvantage of Encapsulation.

    1.it is increases length of the code and slow down execution.

Every Data member should be declared as private and for every member we have to maintain 
 getter and setter method.


learn java online free and learn java programming this is the
best way to learn java. this is best java tutorial.in this tutorial          
all the programs start with hello java,hello world.Enterprise applications         
programming language

Abstraction in Java - Learn Java


Abstraction in Java - Learn Java

Abstraction in oops with Example

  1. Hide internal implementation and just show the set of service
          is called abstraction. 

The Main Advantage of Abstraction
  1. We can achieve security as we are not highlighting our internal implementation.
  2. it improves maintainability of the application.
  3. it improves modularity of the application.
  4.  it improves easyness to use our system.
Use of interface we can implement abstraction.


learn java online free and learn java programming this is the
best way to learn java. this is best java tutorial.in this tutorial          
all the programs start with hello java,hello world.Enterprise applications         
programming language

Friday, September 27, 2019

What is data hiding in a program? - Learn Java


What is data hiding in a program? - Learn Java

  • Over internal data should not go out directly that is outside person can't access our internal data directly.
  • By using private modifier we can implement data hiding .  

Data Hiding In Java Example Program.

EXAMPLE 
       
        class Account
       {
           private double balance;
           ---------------------------;
          ----------------------------;
        }
    After providing proper username and password only, we can access our Account 
    information.
   Main advantage of data hiding is security.


learn java online free and learn java programming this is the
best way to learn java. this is best java tutorial.in this tutorial          
all the programs start with hello java,hello world.Enterprise applications         
programming language

Wednesday, September 18, 2019

Object Oriented Programming(OOPs)Concept in Java-Learn Java

Object Oriented Programming(OOPs)Concept in Java-Learn Java

learn java online free and learn java programming this is the
best way to learn java. this is best java tutorial.in this tutorial          
all the programs start with hello java,hello world.Enterprise applications         
programming language    

  1. Abstraction.
  2. Data Hiding.
  3. Encapsulation.
  4. Tightly encapsulation class.
  5. IS-A Relationship(Inheritance).
  6. HAS-A Relationship.
  7. Method Singnature
  8. Polymorphism
  9. Static Control Flow.
  10. Singleton Class.
  11. Fectory Method.       

Java multithreading Tutorial with Real-Life-Example(2025)

  Java Multithreading Tutorial with Real-Life Examples (2025) Meta Description: Learn Java Multithreading with real-world examples. Und...