Posts

Showing posts with the label JAVA

Encapsulation

Image
Encapsulation is simply "the data protection".  So encapsulation is used to protect data in variables. We can restrict data by using the "private" modifier. And then we can use get and set methods to access those data. As variables are private,we should use public methods to access them. In encapsulation,we access protected variables by using get and set methods. eg :- class Book{     private int number;     private String name;     private String author;     }     class Run{     psvm(String[]args){     Book b = new Book();     b.number=12345;     b.name="harry potter";     b.author="J.K.Rowlin";     }     }     public void setnum(int number) {     this.number=number;     }     public int getnum() {     return number;     }          output - 12345

Variable and Method types in JAVA

Image
  Variable types..... 1) Local variables/method local they are inside a method. eg :- class A {         void a () {         String x ;         }           } 2) Instance variables the variables inside a class. but cannot be inside a method. eg :- class B {          String x ;         } 3) Static variables/class variables Static variables must be in a class. but it cannot be inside a method. eg :- class A {          static String x ;         }            Method types..... 1) Pre - defined methods. - pre built methods in JDK - public static  void main (String[]args) {} - print (); - get (); - set (); 2) User - defined methods. - methods created by the user. -2 types static methods - static void draw () {} non static nethods - void eat () {} / void write () {}   ...

Conditions and Loop

Image
 Conditions..... Java has 3 types of conditional statements. If else else if If a specified condition is true, we use "if" to execute a code block  under it. If the same condition is false, we use "else" to specify a block of code to be executed. And then we can use "else if" to specify a new condition to test if the 1st condition is false. The above conditions return only boolean values.(true/false) eg :- class A{             if(true){                sout ("A"); }             else {                sout ("B"); }          } eg :- class A{             if(false){                sout ("A"); }             else if{                sout ("B"); }           ...

Interface

Image
  What is an interface according to your knowledge ? Is it a thing that a user can see ? or is it the thing we interact when we use various software applications and systems ? But "Interface" in JAVA is totally different from that. Interface is same as a class in JAVA but it is not a class.  We use this concept because we haven't any way to do multiple inheritance. So that we can use "Interfaces" to inherit many classes. Like we use "extends" in classes,we use the keyword "implements" in Interfaces. Create an Interface..... interface Human { } Inherit an Interface..... eg  :- class Human {           }          interface Man{          }          class Student extends Human implements Man{          } eg :- class A{}      interface C{}      interface D{}      interface E{}      class B extends...

Abstraction

Image
What is a meaningless method ? when we inherit sub classes and super classes,we have to create a meaningless method/s to override. meaningless/blank methods haven't a body. It only used to override methods as our wish and we cannot include specific methods inside meaningless methods as it is only used to overriding. We write our codes inside the methods of sub classes according to our necessity after overriding. When we use "Abstraction" ? When we are unable to give specific codes inside a method that is being created in a certain instance(Meaningless methods) ,we use "Abstraction" to keep that type of methods. For that, "Abstract" keyword is used. Keypoints  We use abstraction to keep meaningless methods. Only abstract class can keep abstract methods. Abstract class can contain both abstract methods and non-abstract methods. Objects cannot be created by using abstract classes. Therefore constructors cannot be created. But we can run methods by using t...

Polymorphism

Image
 Polymorphism The process of calling the sub class methods by using the super class reference with the help of Overriding,Inheritance and Casting concepts. After we override a method of a super class inside a subclass,we can do casting and then through the super class reference,we can call the sub class method. We can perform many tasks in different classes by using this concept. This simply means "Many Forms". Assume that there is a company.It stores Biscuits and Cakes seperately.For that we have to develop a system to store them in the correct manner. As Java is a compiler,it executes its codes line by line. Now if we have either cakes or biscuits,the should have to store in their correct places. So for that we can use polymorphism with casting,inheritance and method overriding concepts. We can create seperate objects for those sub classes. Super class is called and then the sub class is executed. This is a simple idea about how we can use polymorphism practically. Let us d...

Casting

Image
Casting Integer Hierarchy long int short char byte Floating Hierarchy   double float According to the above sequence the data types that are in the highest point have the highest power than lowers. Upcasting We can understand upcasting simply as giving the subclass object to the super class variable. In upcasting we cannot access higher types by lower types. But lower types can be accessed by higher types. Downcasting Converting the sub class object added to the super class variable back into sub class variable. This is simply giving the super class variable to the sub class variable. For this,sub class object is used as parameter.

Method Overriding & Super Keyword

Image
  Method Overriding Simply,method overriding is changing the body of a method that is coming from super class to a a sub class . It occurs when the sub class has the same method as in the super class. In the " inheritance " article we could see that a method in the super class can be accessed by the sub class. But if the both classes have 2 methods with the same name? What can we do? In such instances,the method in the sub class overrides the method in the super class.That concept can be called as " Method Overriding in JAVA ". In overriding,the methods that are related to the object are executed always. Let me explain it by using the following example. When studying the above code,we can identify the class Student as the super class and the class Child as the sub class. And also,the method learn() is included in both classes. Here we have created an object called " baby " by using the sub class Child. So when we call the method learn() by using the o...

Inheritance

Image
  Inheritance What is the meaning of Inheritance ? Simply,inheritance is owning or belonging something/s from one person/generation to another person/generation.It may be anything. What is Inheritance in JAVA ? Taking the things/properties in one JAVA class to another JAVA class is called as Inheritance . What are two categories of Inheritance Concept ? sub class (child) - the class that inherits from the initial class. super class (parent) - the class that is used to inherit other classes. How do we inherit one class from another class ? First of all we should create a relationship between 2 classes. For that,we use the keyword  extends . If we consider the process of manufacturing a new car as an example, that car may contain newest features and it will include the   features of its previous version. So, the oldest version can be identified as the super class and the modern version will be the sub class. The older version has a colour,name,lights,..etc.Actually they're...

Method Overloading in JAVA

Image
Method Overloading  Method Overloading Concept is used in JAVA when there are many methods inside the source code with the same name in the same class. A JAVA code will have multiple methods with the same name while its parameters are different. We can do method overloading by changing the parameter. It is known as Method Signature . So multiple methods can have same name with different parameters. We should pass relevent agruments to the parameter when the method is being called. Consider the following methods. void one() { } void one(String a) { } void one(int x) { } void one(float x,float y) { } We can see there are 4 methods with the same name but with different parameters. Now you can call them by giving suitable arguments. one (); one ("hello world !"); one (567); one (56.9,45.3); Simply, this is the way of using many methods with the same name in a same JAVA class in programming. I think you all are able to understand how to use Method Overloading Concept in JAVA. S...

Parameters,Arguments & Constructor

Image
 Parameters and Arguments Today we're here to discuss about parameters and arguments in JAVA. Let's look at it by studying the above example. Parameter is there to keep inputs and outputs. Information can be passed to a method by using parameter list. Parameters act as variables inside a method. We can add many Parameters by seperating with commas. When a   parameter is passed to a method,it is called an argument. Arguments in JAVA are actually the values that are passed to the variables in a method. Return type is used to return the output. In JAVA, the default return type is "void".It can return any data type or String value. And also String or any data type can be used as the return type.But we must use the "return" keyword inside the method. eg:- 1) int area (int x,int y){         int z=x*y;         return z;         System.out.print(z);         }         area (7,13); ...

Object Creation and Data types in JAVA

Image
  Object Creation and Data Types in JAVA + In JAVA,we can generate an object if only there is a class. + Object is created from a class. + And also only a class can be converted into an object. + Everything in JAVA is based on objects and classes along with its arrtibutes and methods. + Let's see how to create an object step by step. 👉  Create a Class public class Main {    int x=5; } 👉  Create an Object public class Main{    int x=5;    public static void main(string[]arg) {         Main obj = new Main();         System.out.println(obj.x);    } } Data Types Formats or types that are being used to keep data are "Data Types". In JAVA,a variable must be written with a specified data type that is suitable to it. Data types are divided into 2 parts. (i) Primitive data types byte(integer) char(integer) short(integer) int(integer) long(integer) float(floating point) double(floating point) boolean(...

An Introduction to JAVA

Image
An Introduction to Java In this article we'll seek into following keypoints. What is JAVA ? OOPC (Object Oriented Programming Concept) JAVA Standards Operators in JAVA Class & Object 1. What is JAVA ? JAVA is a high-level,general purpose and powerful programming language. It is object oriented language and mainly based on OOPC (Object Oriented Programming Concept). JAVA is machine independent. It was developed at Sun MicroSystems which was purchased by Oracle in 2010. JAVA is used to develop software that can run on mobile,desktop and servers. 2. OOPC (Object Oriented Programming Concept) As you know,an object is referred to anything in this world. in JAVA hierachy,object is at the top of the positions and that's why it is called "Object Oriented Language". In simply,Object Oriented Programming is a concept that uses  objects for programming. It basically implements real world entities to programs. Now-a-days OOPC makes proramming an easy task than earlier. In ear...