Java is an Object-Oriented Language. As a language that has the Object Oriented feature

  1. Objects
  2. Classes
  3. Polymorphism
  4. Inheritance
  5. Encapsulation
  6. Abstraction

Objects in Java :

  • If we consider the real-world we can find many objects around us, Cars.
  • If you compare the software object with a real world object, they have very similar characteristics.
  • Software objects also have a state and behavior. A software object's state is stored in fields and behavior is shown via methods.
  • So in software development, methods operate on the internal state of an object and the object-to-object communication is done via methods.

Classes in Java

A class is a blue print from which individual objects are created.
A sample of a class is given below:

class Emp
{
Int code;
String  name;
Void get data(int c,String n)
{
Code =c;
Name =n;
}
Void Show data()
{
System.out.println(“code=” +code);
System.out.println(“name=” +name);
}
}
Classs  Employee
{
Public static void main(String args[])Throws IOException
{
Int code;
String name;
Emp  e= new  Emp();
System.out.println(“Enter code”);
e.get data(code,name);
e.show Data();
}
}

Local variables: Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
Instance variables: Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.
Class variables: Class variables are variables declared within a class, outside any method, with the static keyword. A class can have any number of methods to access the value of various kinds of methods. In the above example,


Java Inheritance :

Inheritance is one of the feature of Object-Oriented Programming (OOPs). Inheritance allows a class to use the properties and methods of another class. In other words, the derived class inherits the states and behaviors from the base class. The derived class is also called subclass and the base class is also known as super-class. The derived class can add its own additional variables and methods. These additional variable and methods differentiates the derived class from the base class.

Inheritance is a compile-time mechanism. A super-class can have any number of subclasses. But a subclass can have only one superclass. This is because Java does not support multiple inheritance.
Inheritance in java is a mechanism in one object all the properties and behaviors of parent.The idea behind inheritance in java that you can create a new class That are built upon existing class. When you inherent existing class you can reused methods and fields of method and you can add new methods and fields Inheritance represents the parent , child relationships

Types of inheritance in java

  • multilevel inheritance
  • multiple inheritance
  • hybrid inheritance
  • hierarchical inheritance

Multilevel Inheritance

Multilevel inheritance refers to a mechanism in OO technology where one can inherit from a derived class, thereby making this derived class the base class for the new class. As you can see in below flow diagram C is subclass or child class of B and B is a child class of A. For more details and example refer – Multilevel inheritance in Java.


Multiple Inheritance

“Multiple Inheritance” refers to the concept of one class extending (Or inherits) more than one base class. The inheritance we learnt earlier had the concept of one base class or parent. The problem with “multiple inheritance” is that the derived class will have to manage the dependency on two base classes.


Hybrid Inheritance

In simple terms you can say that Hybrid inheritance is a combination of Single and Multiple inheritance. A typical flow diagram would look like below. A hybrid inheritance can be achieved in the java in a same way as multiple inheritance can be!! Using interfaces. yes you heard it right. By using interfaces you can have multiple as well as hybrid inheritance in Java.


Hierarchical Inheritance

In such kind of inheritance one class is inherited by many sub classes. In class B,C and D inherits the same class A. A is parent class (or base class) of B,C & D. Read More at – Hierarchical Inheritance


Uses inheritance in java

  1. For Method Overriding (so runtime polymorphism can be achieved).
  2. For Code Reusability.

Syntax of Java Inheritance

class Subclass-name extends Superclass-name  
{  
   //methods and fields  
} 

The extends keyword indicates that you are making a new class that derives from an existing class. In the terminology of Java, a class that is inherited is called a super class. The new class is called a subclass.

class Person
{
class Parent
{  
 String parentname=”K.Srinivasarao”;
}  
class Child extends Parent
{  
String childname=”K.V.S.Durga”;
}
 public static void main(String args[])
{  
   Child c=new Child();  
   System.out.println("Parent Name is :"+c.parentname);  
   System.out.println("Child Name is :"+c.childname);  
}  
}
 

Java Overriding Methods

In the previous chapter, we talked about superclasses and subclasses. If a class inherits a method from itssuperclass, then there is a chance to override the method provided that it is not marked final. The benefit of overriding is: ability to define a behavior that's specific to the subclass type which means a subclasscan implement a parent class method based on its requirement. In object-oriented terms, overriding means to override the functionality of an existing method.

Example:
class Anji
{
public void move(){
System.out.println("Anji can move");
}
}
class veeru extends Anji
{
public void move()
{
System.out.println("veeru can walk and run");
}
}
public class Testveeru
{
public static void main(String args[])
{
Anji a =new Anji();
Anji b =new veeru();
a.move();
b.move();
}
}

Advantage of method overriding

The main advantage of method overriding is that the class can give its own specific implementation to a inherited method without even modifying the parent class(base class). Method Overriding in dynamic method dispatch Dynamic method dispatch is a technique which enables us to assign the base class reference to a child class object


Rules for Method Overriding:

  1. applies only to inherited methods
  2. object type (NOT reference variable type) determines which overridden method will be used at runtime
  3. Overriding method can have different return type (refer this)
  4. Overriding method must not have more restrictive access modifier
  5. Abstract methods must be overridden
  6. Static and final methods cannot be overridden
  7. Constructors cannot be overridden
  8. It is also known as Runtime polymorphism.

Overloading Methods:

The max method that was used earlier works only with the int data type. But what if you need to find which of two floating-point numbers has the maximum value? The solution is to create another method with the same name but different parameters, as shown in the following code:

  1. To call an overloaded method in Java, it is must to use the type and/or number of arguments to determine which version of the overloaded method to actually call.
  2. Overloaded methods may have different return types; the return type alone is insufficient to distinguish two versions of a method.
  3. When Java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call.
  4. It allows the user to achieve compile time polymorphism.
  5. An overloaded method can throw different exceptions.
  6. It can have different access modifiers.
EXAMPLE

Import  java.io.*;
Class  TArray
{
Static int sum(int a.int b);
{
Return (a+b);
}
Static int sum (int a,int b, int c);
{
Public static void main(String  args[])Throws IOException
Int  a,b,c;
System.out.println(“A+B=” +sum (a,b));
System.out.println(“A+B+c=”+sum (a,b,c));
} 
}

Rules for Method Overloading

  1. Overloading can take place in the same class or in its sub-class.
  2. Constructor in Java can be overloaded
  3. Overloaded methods must have a different argument list.
  4. Overloaded method should always be the part of the same class (can also take place in sub class), with same name but different parameters.
  5. The parameters may differ in their type or number, or in both.
  6. They may have the same or different return types.
  7. It is also known as compile time polymorphism.

Java Polymorphism

Polymorphism is the ability of programming languages to present the same interface for differing underlying data types. A polymorphic type is a type whose operations can also be applied to values of some other type. Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism inOOP, occurs when a parent class reference is used to refer to a child class object.

Polymorphism is the capability of a method to do different things based on the object that it is acting upon. In other words, polymorphism allows you define one interface and have multiple implementations. I know it sounds confusing. Don’t worry we will discuss this in detail.

  • It is a feature that allows one interface to be used for a general class of actions.
  • An operation may exhibit different behavior in different instances.
  • The behavior depends on the types of data used in the operation.
  • It plays an important role in allowing objects having different internal structures to share the same external interface.
  • Polymorphism is extensively used in implementing inheritance.
Example:
Class  person
{
Class  parent
{
String  parent  name=”Srinivasa  rao”;
}
Class  child extends parent
{
String  child  name=”VEERU”;
}
Public  static void main (String args[])
{
Child  c=new  child ();
System.out.println(”parent name is: “+c,parent name);
 System.out.println(“child  name is: “+c,child  name);
}
}

Encapsulation in Java

Encapsulation in java is a process of wrapping code and data together into a single unit, for example capsule i.e. mixed of several medicines.
We can create a fully encapsulated class in java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it.
The Java Bean class is the example of fully encapsulated class.

The whole idea behind encapsulation is to hide the implementation details from users. If a data member is private it means it can only be accessed within the same class. No outside class can access private data member (variable) of other class. However if we setup public getter and setter methods to update (for e.g. void setSSN(int ssn))and read (for e.g. int getSSN()) the private data fields then the outside class can access those private data fields via public methods. This way data can only be accessed by public methods thus making the private fields and their implementation hidden for outside classes. That’s why encapsulation is known as data hiding.


Advantage of Encapsulation in java

By providing only setter or getter method, you can make the class read-only or write-only.
It provides you the control over the data. Suppose you want to set the value of id i.e. greater than 100 only, you can write the logic inside the setter method. Simple example of encapsulation in java
Let's see the simple example of encapsulation that has only one field with its setter and getter methods.

	//save as Student.java  
	package com.javatpoint;  
	public class Student{  
	private String name;  	   
	public String getName(){  
	return name;  
	}  
	public void setName(String name){  
	this.name=name  
	}  
	}  
	/save as Test.java  
	package com.javatpoint;  
	class Test{  
	public static void main(String[] args){  
	Student s=new Student();  
	s.setname("vijay");  
	System.out.println(s.getName());  
	}  
	}  

News Walls


Copyrights © 2016. All Rights Reserved to Learn Computers Home, India. //Site Designed.