Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Basic Concepts of Java Programming

In this article we are going to learn about Basic Concepts of Java Programming.

Today if we have a glance over the fields of study, we will come to know that almost every field uses information technology, every field that is advancing primarily depends on the advancement in the field of IT., in which software side has its own importance that can never be ignored.

The software side has brought each and every thing to the fingertip, and things that seemed to be impossible are easily seen and practiced in the daily life. How these goals are achieved, that is actually the power of the programming. There are hundreds of programming languages today. The language we will discuss with you is the java programming language.

In the World of programming Language, There are mainly two types of programming Languages.

1. Procedure oriented.

One Of The Most Known Procedure Language with which We Are Familiar is C language. Its Follow Top Down approach. It depends on Procedures to complete the Task. Procedure Oriented programming language is also known as Imperative programming Language.

2. Object Oriented.

Another well known Programming Language type is Object Oriented Programming such as Java, c++, python, c# etc. Object oriented Programming Languages Working upon Object hence it is known as object oriented programming language. It separates the code logically using classes. The basic idea behind object-oriented programming is to create Object in code, the object that has different Properties and Methods.

Some basic concepts of JAVA

Class: In the object oriented programming language, we can define a class as a blueprint. Class is a collection of properties and Methods. In a class, we can define access specifies of class Members. We can Say Class is a template for objects.

Syntax of class is given below:

Syntax:

  [Access Specifier] Class [ClassName]
  {
                  // Data Members or Attribute or Property
   
                  [Access Specifier] [Datatype] [ Data Member 1 ];
                  [Access SpeciFier] [Datatype] [ Data Member 2 ];
                                                 ...
                                                 ...
  [Access SpeciFier] [Datatype] [ Data Member n];
                  
                  // Methods 
   
                  [Access Specifier] [Retrun Type] MethodName1(parameter){}
                  [Access Specifier] [Retrun Type] MethodName2(parameter){}
                                                 ...
                                                 ...
                  [Access Specifier] [Retrun Type] MethodNamen(parameter){}
  }

In the above syntax, Access Specifier is declared the scope of the class where it is accessible. Class is the keyword and class name is the unique name of the class. Same like class we define methods and variables using Access specifier and data type, where data type is a type of variable which kind of value going to be store. And return type of methods indicates which kind of value method will return. Example Of class Is given Below:

Listing 1: ClassExample:

  public class Sample
  {
                  int var1; // integer Type Variable with default access specifier.
  publicString var2; // String Type Variable with public specifier.
   
  public void showData() // Method with Public specifier.
  {
                 System.out.println(“Print What you Want To Display..”);
  }
  }

Above example Demonstrates the simple creation of class in java. In which the class name is Sample and it has public access specifier that indicates that this class can be accessed from anywhere in the package and another package. Sample class has 2 variables and 1 method that are known as Data Member of class. If we didn’t provide any specifier in java declaration then it should be default specifier. It is accessible in current package. This is the common structure of class in Java.

Object: Object is commonly an instance of a class. In java Object is also known as instance. Every object in java has its own state. So if we declare two object x and y then both object has different state. Object is the core concept of object oriented programming language.

Syntax to declare the object of class in java is:

Syntax:

  [ Class Name ] objectName;
  Intilize Object:
  ObjectName = new [ClassName]();  OR
  [Class Name] Object=new ClassName();

In above syntax Class name is a Name of class for whom you want to create an object. Object name can be anything that you want as it needs to be unique [start from A-Z, a-z,and ‘ _’ ].

Object Example is Given Below.

Listing 2: ObjectExample:

  Sample obj1; // Here Object is created of Above Mention Sample Class.
  obj1=new Sample(); // Here we initialize the obj1 by using ‘new’ keyword.

OR

Sample obj1=new Sample();  // here We create and Initialize object.

Inheritance: In java when one class shares properties with another class this concept is known as Inheritance. In java multiple inheritance is not supported. Inheritance is the fundamental key of object oriented programming. Super class and sub class relationship can be established through Inheritance.

Syntax Of Inheritance

Syntax:

  class [DerivedClass Name]  extends [Base Class Name]

Here in above syntax Derived Class Name indicates your class name that you want to create. And extends is a keyword that is used to achieve inheritance in java. And base class name is the name of class that you are going to inherit.

Example of Inheritance

Listing 3: Test.Java

  class Accessories
  {
                  String name;
                  String description;
                  double price;     
                  
                  public Accessories(String name,Stringdesc,double price)
                  {
                                 this.name=name;
                                 this.description=desc;
                                 this.price=price;
                  }              
                  public String getName()               
                  {
                                 return name;
                  }
                  
                  public void setName(String name)         
                  {
                                 this.name=name;
                  }
                  public String getDescription()    
                  {
                                 return description;
                  }
                  
                  public void setDescription(String desc) 
                  {
                                 this.description=desc;
                  }
                  
                  public double getPrice()
                  {
                                 return price;
                  }
   
                  public void setPrice(double price)
                  {
                                 this.price=price;
                  }
  }
   
  class Laptops extends Accessories
  {
                  intlaptopId;
                  String companyName;  
   
                  public Laptops(String 
                  name,Stringdesc,doubleprice,intlaptopId,Stringcompanyname)
                  {
                                 super(name,desc,price);             
                                 this.laptopId=laptopId;
                                 this.companyName=name;
                  }                              
                  publicintgetLaptopId()
                  {
                                 returnlaptopId;
                  }
                  public void setLaptopId(intlaptopId)
                  {
                                 this.laptopId=laptopId;
                  }              
                  public String getCompanyName()
                  {
                                 returncompanyName;
                  }
                  public void setCompanyName(String companyName)
                  {
                                 this.companyName=companyName;
                  }
   
  }
  class Mobile extends Accessories
  {
                  intmobileId;
                  String model;
                  
                  public Mobile(String name,Stringdesc,doubleprice,
                   intmobileId,String model)
                  {
                                 super(name,desc,price);             
                                 this.mobileId=mobileId;
                                 this.model=model;
                  }              
                  publicintgetMobileId()
                  {
                                 returnmobileId;
                  }
                  public void setMobileId(intmobileId)
                  {
                                 this.mobileId=mobileId;
                  }              
                  public String getModel()
                  {
                                 return model;
                  }
                  public void setModel(String model)
                  {
                                 this.model=model;
                  }
   
  }
  public class Test 
  {
  public static void main(String args[])
      {
            Mobile mobile=new Mobile("HeadSet","Wireless",299.0,1,"Nokia 5800");
            System.out.println( "Name         : " + mobile.getName() +
            "\nDescription :" +mobile.getDescription() +
            "\nPrice       :"+mobile.getPrice()+
            "\nMobile Id   :"+mobile.getMobileId()+
            "\n Model      :"+mobile.getModel());
                                                                                                                
            mobile.setPrice(365.0f); 
            System.out.println("\n\nName      : " + mobile.getName() +
              "\nDescription :" +mobile.getDescription() +
              "\nPrice       :"+mobile.getPrice()+
              "\nMobile Id   :"+mobile.getMobileId()+
              "\nModel       :"+mobile.getModel());
   
             Laptops laptop=new Laptops("RAM","DDR2 1 Gb",1299.0,1,"IBM ThinkPad");
             System.out.println("\n\nName      : " + laptop.getName() +
                "\nDescription :" +laptop.getDescription() +
                "\nPrice       :"+laptop.getPrice()+
                "\nLaptop Id   :"+laptop.getLaptopId()+
                "\nCompnyName :"+laptop.getCompanyName());
                                 
              laptop.setPrice(1465.0f); 
              System.out.println("\n\nName      : " + laptop.getName() +
                  "\nDescription :" +laptop.getDescription() +
                   "\nPrice       :"+laptop.getPrice()+
                    "\nLaptop Id   :"+laptop.getLaptopId()+
                     "\nCompnyName :"+laptop.getCompanyName());
                                                                                                                               
           }   
  }

Output: Listing 3: Test.java

Name : HeadSet
Description :Wireless
Price :299.0
Mobile Id :1
Model :Nokia 5800

Name : HeadSet
Description :Wireless
Price :365.0
Mobile Id :1
Model :Nokia 5800

Name : RAM
Description :DDR2 1 Gb
Price :1299.0
Laptop Id :1
CompnyName :RAM

Name : RAM
Description :DDR2 1 Gb
Price :1465.0
Laptop Id :1
CompnyName :RAM

Here in above example we saw that the main purpose of inheritance is reusability of code.Accessories class is used by Mobile and Laptops class. So here Accessories class is a parent class for Mobile and Laptops class. Here Mobile class and Laptops class use the properties of Accessories class so it’s the main concept of inheritance. The Code of Accessories class can be used by any other class that inherits this particular class. In inheritance you can call the constructor of base class using ‘super’ keyword. And ‘super’ keyword must need to be the first line of constructor otherwise it gives compile time error. As we know java does not provide multiple inheritance but still we can achieve multiple inheritance in java using “Interface”.

Interface: An interface is like a class in java but there are difference between class and interface. As interface is a collection of methods or we can say collection of abstract methods. An interface can have any number of methods. Class can inherit the interface using the ‘implements’ keyword. We can achieve multiple inheritance using interfaces. Class can inherit more than one interface. Interface have all methods abstract so we can say it is a fully abstract class. Class that inherit interface must need to implement all the methods of interface. In interface we can only declare or give prototype to method we can not define method inside the interface.

Syntax for Interface:

  [Access Specifier] interface [InterfaceName]
  {
                  [Access Specifier] [Retrun Type] MethodName1(parameter){}
                  [Access Specifier] [Retrun Type] MethodName2(parameter){}
                                                 ...
                                                 ...
                  [Access Specifier] [Retrun Type] MethodNamen(parameter){}
  }

The above syntax defines how to create interfaces in java. Interface keyword is use to create interface in java. Interface name is a name of your interface that can be anything that you want. But it first character must be alphabet or underscore, otherwise it create compile time error. The Best Example of interface in java is Runnable interface. When we already extended one class and we need to implement threading in our class what we can do?

In above scenario we use interface name Runnable. Using Runnable interface we get the multiple inheritance functionality.

Abstraction: In Java abstraction means to hide something. When One class hides properties from another we refer to this as abstraction. We can say abstraction provides hiding facility in java.

Encapsulation: In java encapsulation means binding or wrapping different classes or we can say code or data together as a single unit. In java full encapsulated class is java bean class. Java provides encapsulation using packages and classes.

Polymorphism: In java, method with the same name can perform different tasks. This feature is known as polymorphism. In java we can achieve polymorphism using method overloading and method overriding. There iare two types of polymorphism in java. In polymorphism object or entity or method can perform multiple action.

1. Compile Time Polymorphism.

The best example of compile time polymorphism is method overloading and constructor overloading. In method overloading method have same name but different arguments. Overloading increases the code readability.

Example of method overloading:

Listing 4: TestOverLoading.java

   
  public class TestOverLoading
  {
   
  privateintbookId;
                  private String bookName;
                  private String author;
                  private String path;
                  private double price;
                  
                  // Constructor Overloading (Compile Time Polymorphism).
                  
                  publicTestOverLoading()
                  {
                                 //Default Constructor
                                 bookId=0;
                                 bookName=null;
                                 author=null;
                                 path=null;
                                 price=0.0;
                  }
                  
                  publicTestOverLoading(intbookId,StringbookName,Double price)
                  {
                                 this.bookId=bookId;
                                 this.bookName=bookName;
                                 this.author=null;
                                 this.path=null;
                                 this.price=price;
                  }
                  
                  publicTestOverLoading(intbookId,StringbookName,
                    Stringauthor,Stringpath,Double price)
                  {
                                 this.bookId=bookId;
                                 this.bookName=bookName;
                                 this.author=author;
                                 this.path=path;
                                 this.price=price;
                  }
                  
                  
                  // Method Overloading (Compile Time Polymorphism).
                  
                  public double price()
                  {
                                 return price;
                  }
                  
                  public double price(intqty)
                  {
                                 return price*qty;
                  }
                  
                  public double price(float rate)
                  {
                                 return price+(price*rate)/100;
                  }
                  
                  // Getter And Setter Method Of Class 
                  // To get And Set The Valu of Member of class.
                  
                  public void setBookID(intbookId)
                  {
                                 this.bookId=bookId;
                  }
                  public void setBookName(String bookName)
                  {
                                 this.bookName=bookName;
                  }
                  public void setPath(String path)
                  {
                                 this.path=path;
                  }
                  public void setAuthor(String author)
                  {
                                 this.author=author;
                  }
                  public void setPrice(double price)
                  {
                                 this.price=price;
                  }
                  
                  
                  publicintgetBookID()
                  {
                                 returnbookId;
                  }
                  public String getBookName()
                  {
                                 returnbookName;
                  }
                  public String getPath()
                  {
                                 return path;
                  }
                  public String getAuthor()
                  {
                                 return author;
                  }
                  public double getPrice()
                  {
                                 return price;
                  }
   
                  /* Here We Override toString method of base Class Object. */
                  
                  public String toString()
                  {
                                 return    "Book Id     : " + getBookID() +
                                                                 "\nBook Name   : " + getBookName() +
                                                                 "\nBookAuthor : " + getAuthor() +
                                                                 "\nBook Path   : " + getPath() +
                                                                 "\nBookPrice  : " + getPrice() ;                  
                  }
                                 
                  
                  public static void main(String args[])
                  {
                                 /* Here testOveloading object Call a default Constructor */
                                 TestOverLoadingtestOveloading=new TestOverLoading(); 
                                 
                                 testOveloading.setBookID(1);
                                 testOveloading.setAuthor("Author-1");
                                 testOveloading.setBookName("Book-1");
                                 testOveloading.setPath("\\Books\\Book1.pdf");
                                 
                                 /* Here testOverloading object Call Overloaded Method price without Any argument */
                                 testOveloading.setPrice(200f);                
                                 
                                 System.out.println(testOveloading.toString());
                  
                                 /* Here testOveloading Object Call a With 3 parameter Constructor */
                                 testOveloading=new TestOverLoading(2,"Book-2",399.0); 
                                 
                                 testOveloading.setAuthor("author-2");
                                 testOveloading.setPath("\\Books\\Book2.pdf");
                                 
                                 System.out.println("\n\n"+testOveloading.toString());
                                 
                                 /* Here testOveloading object call Overloaded Method one integer argument.*/
                                 System.out.println("\n\nTotal of 6 Books(book*"+testOveloading.getPrice()+") :"+testOveloading.price(6));
                                 
                                 /* Here testOveloading object call Overloaded Method five integer argument.*/
                                 testOveloading=new TestOverLoading(3,"Book-3","Author-3","\\Books\\Book3.pdf",399.0);
                                 System.out.println("\n\n"+testOveloading.toString());
                                 
                                 /* Here testOveloading object call Overloaded Method one float argument.*/
                                 System.out.println("\nPrice With Tax: "+testOveloading.price(10.0f)); 
                  }
  }
  

Output of listing 4: TestOverLoading.java

Book Id : 1

Book Name : Book-1

Book Author : Author-1

Book Path : \Books\Book1.pdf

Book Price : 200.0

Book Id : 2

Book Name : Book-2

Book Author : author-2

Book Path : \Books\Book2.pdf

Book Price : 399.0

Total of 6 Books(book*399.0) :2394.0


Book Id : 3

Book Name : Book-3

Book Author : Author-3

Book Path : \Books\Book3.pdf

Book Price : 399.0

Price With Tax: 438.9

In the above example we saw method overloading. TestOverLoading class constructor is overloaded and method, name price() overloaded with different arguments.

2. Run Time Polymorphism.

Best example of runtime polymorphism is method overriding. When in inheritance Derived class has the same prototype of base class method is called a method overriding. The best example of method overriding is the method of Object class toString().

Example of Method Overriding:

Listing 5: OverRiding.java

   
  classFoodGrain
  {
                  public void getDetail()
                  {
                                 System.out.println("\n\n The method of Base Class FoodGrain is Call. ");
                  }
  }
   
  classBajra extends FoodGrain
  {
                  public void getDetail()
                  {
                                 System.out.println("\n\n The method of sub Class Bajra is Call. ");
                  }              
  }
   
  class Wheat extends FoodGrain
  {
                  public void getDetail()
                  {
                                 System.out.println("\n\n The method of sub Class Wheat is Call. ");
                  }              
  }
   
  class Sorghum extends FoodGrain
  {
                  /* Here We have not override the method of the Base Class.
                   *so it call the Base Class Method */
  }
  public class OverRiding
  {
   
  publicOverRiding(){ }
   
  public static void main(String[] args) 
      {
                  FoodGrainfoodGrain=new FoodGrain();
                  foodGrain.getDetail();
                  foodGrain=new Bajra();
                  foodGrain.getDetail();
                  foodGrain=new Wheat();
                  foodGrain.getDetail();
                  foodGrain=new Sorghum();
                  foodGrain.getDetail();
      }
  }
  

Output Listing 5: OverRiding.java

The method of Base Class FoodGrain is Call.

The method of sub Class Bajra is Call.

The method of sub Class Wheat is Call.

The method of Base Class FoodGrain is Call.

Above example of method overriding shows how method overriding works. Here FoodGrain is a base class and Bajra, Wheat and Sorghum are sub class of FoodGrain. FoodGrain class have method, name getDetail() that is overridden in two derived classes Bajra and Wheat.

Conclusion:

In this tutorial we learn the basic concept of java, and got the idea how to create classes and objects. We also explored the concept of Inheritance. As we know java is Object Oriented Programming Language but we still didn’t say that Java is Pure Object Oriented Programming as Java has a primitive data types such as int, char, float etc. so java is not pure object oriented programming language.



Computer system engineer who has 4 year of experience in the field our web-development ,FPGA, and java programming.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login