Java4teachus

Tuesday, June 25, 2019

Define Inheritance Types Of Inheritance In Java

Inheritance

Introduction

Inheritance is one of the main features of OOPS and makes it is development reusable.

Definition

The processes of obtaining data members & methods (features) from one class to another class is called inheritance. (or)  The process of deriving the one class features into another class is called as inheritance.

Super Class and Base Class

The class which is giving the data members & methods is called "Base class / super  class/ parent class". where the class is taking data members & methods is called as “Derived class /  Child class”.

Inheritance concept always follows logical memory management. This management is base class features are available in derived class without taking any physical and without taking physical code (in the content of we can just reuse the features of base class).

inheritance-in-java.jpeg
Inheritance concept is also known as re-usability (or) extended class (or) derivation (or) sub class.

Advantages / Benefits  Of  Inheritance 

If we develop any Java application in inheritance concept we get the following advantages. 
  1. Application memory space is less. 
  2. Application development time is less. 
  3. Application execution time is less. 
  4. Application performance is improved. 
  5. Redundancy of code is minimized. 
  6. Able to get the slogan of Java.
Types of  Inheritance (or) Re-Usability Techniques

A pattern or model which makes as to understand how to inherit the feature from the base class to derived class is class inheritance type or reusable techniques.
Types-Of-Inheritanc-In-Java.jpeg
 They are five types of inheritance they are
1)      Single inheritance.
2)      Multi level inheritance.
3)      Hierarchical inheritance.
4)      Multiple inheritance.
5)      Hybrid inheritance.

Tuesday, June 18, 2019

Define Factory method and NameLess Object or annaymous object in java

FACTORY METHOD


Definition:-  A factory method one whose return type is similar to the class name which class in the presents. The purpose of factory method  to create an object of any class without using new operator. We have two types of factory methods. They are

1. Instance factory method
2. Static factory method
Factory-Metod-and-nameless-Objec- in-java.jpeg

➤ Define Nameless object or anonymous class object

A nameless object does not contain any name explicitly. A nameless object is also called anonymous object.

Syntax

new classname( ).Instance methodname();

note:- Nameless object approach concept is applicable for instance method only.

  Q. How do you written an object of current class?

  • Choose the appropriate class name.
  • Choose the appropriate methods in the current class.
  • Current class method must return ‘this’ which is nothing but object of current class.
  • Current class method return type must be the name of current class.

Q. Define a java program by using the factory method and nameless object in java. 

Program:- 

class Test
{
    Test display()
    {
    System.out.println("Hello java");
    return this;           // refers to current class object.
    }
    Test show()
    {
    System.out.println("Java4teachus");
    return this;
    }
    Test write()
    {
    System.out.println("JavaTraining");
    return this;
    }
}
class NameLess
{
public static void main (String[ ] args)
    {
    System.out.println("Display the  named object-----IM");
    Test  t1=new Test();
    t1.display();
    t1.show();
    t1.write();
    System.out.println("Display the  nameless object------IM");
    new Test().display().show();
    }
}
Output :
factory-method-program-or-nameless-object-in-java.jpeg


Source code:

class Sathya
{
    String a;
    Sathya set()
    {
        a="java4";
        System.out.println("insert value="+a);
        return this;
    }
    void increment()
    {
        a=a+"teachus";
        System.out.println("increment value="+a);
    }
}//sathya
class NameLess2
{
public static void main(String args[ ])
    {
        System.out.println("Write the named object------InstanceMember");
        Sathya S =new Sathya();
        S.set();
        S.increment();
        System.out.println("Write the nameless object-----InstanceMember");
        new Sathya().set().increment();
    }
} 
Note:- “this” used must instance content but not in static context ( i.e. static block, static method).

Sunday, June 09, 2019

This Keyword in java

This keyword in Java


➤ This is a keyword which is used in the java programming language.
This is one of reference variable that reffed to current class object. This Keyword is  automatically implicitly  call by jvm during compile time of java program.

 

Purpose Of This keyword In Java?


The main purpose of "This" keyword  is used to differentiate  between the formal parameter in data members of a class. Whenever the formal parameter in data members of a class are simliar then Otherwise we can get the compile time error.

The differentiate between the formal parameters and data members of a class. The data members of a class must be preceded by "This". 

This can be used into 2 ways they are:

  • This. (This Dot)
  • This()  (this off)
This-keyword-In-Java.jpeg


This. (This Dot)
 
This keyword can be used and available in the entire Java program for two purposes.

1. It always points to current class object (it contains reference of current class object).

2. Whenever the formal parameters and Date-Members of class are same then JVM takes ambiguity  problem  ( In order to differentiate between formal parameter and data member of a class must be preceded by "this" Keyword. Otherwise the data member will be treated by "Jvm" as formal parameters.)

Syntax:-

(this. current class data member names)

Without Using Of  THIS Keyword.

Program:-

class Employee
{
int id;
String Name;
float marks;
Employee(int  id String  name, float marks)
{
 id=id;
name=name;
marks=marks;
}
void display()
{
System.out.println("value of id="+id);
System.out.println("value of name="+name);
System.out.println("value of marks="+marks); 
}
} // sub class
class  EmployeeDemo
{
public static void main(String args[])
{
//...Create an object of sub class called Employee... 

Employee e=new Employee("1, lucky,  80.1");
Employee e=new Employee("2, James Gossling,  81.1");
e.display();
e1.dispaly();
}
}

Compling the java Program

Javac EmployeeDemo,java ↵
Java EmployeeDemo 

OutPut:-

0 null values
0 null values

note:- In the above example, formal Parameters and instance data members are same that is why we are using "this" keyword .

Using of This keyword

Program:-
class Employee
{
int id;
String Name;
float marks;
Employee(int  id String  name, float marks)
{
this. id=id;
this. name=name;
this. marks=marks;
}
void display()
{
System.out.println("value of id="+this.id);
System.out.println("value of name="+this.name);
System.out.println("value of marks="+this.marks); 
}
} // sub class 

class  EmployeeDemo
{
public static void main(String args[])
{
//...Create an object of sub class called Employee... 

Employee e=new Employee("1, lucky,  80.1");
Employee e=new Employee("2, James Gossling,  81.1");
e.display();
e1.dispaly();
}
}

OutPut:-

1, lucky, 80.1
2, james Gossling, 81.1


Q. Write a Java program which illustrate of  This Concept..
Program:-

class Test
{
 int a,b;
 Test(int a, int b);
 {
 this.a=a;
 this.b=b;
 this.a=this.a+2;
 this.b=this.b+2;
 a=a+1;
 b=b+1;
 System.out.println("value of a="+this.a);
 System.out.println("value of b="+this.b);
 }
}
class TestDemo
{
 public static void main(string[]argc)
{
 Test t1=new Test (10,20);
 t1.dispaly();
}