December 30, 2017

Inheritance

Inheritance:

Inheritance is the process of acquiring all the properties and behaviors of parent object.
When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields to the new class.
Inheritance represents the IS-A relationship, also known as parent-child relationship.
In general, we call Parent class as Super class and the child class as Sub Class

Syntax of Java Inheritance

class Sub_Class extends Super_Class 
//methods and fields
 The extends keyword indicates that we are creating new class(childClass) that derives from an existing class(parentClass). The meaning of "extends" is to increase the functionality.
Ex:
class Phone{
            int memory=4;
            float screenSize=5.5; 
class iPhone extends Phone{ 
            String manufacturer = "Apple";
            public static void main(String args[]){ 
                        iPhone ip=new iPhone(); 
                        System.out.println("iPhone Memeory is: "+ip.memory);
                        System.out.println("iPhone Screen size is: "+ip.screenSize);
                        System.out.println("iPhone Manufacturer is: "+ip.manufacturer);
            }
}

 Pictorial Representation:

In the above the diagram, iPhone is a subclass of a super Class Phone. Which means iPhone Is A type of Phone.
It means here we created IS-A relation between super class and sub class using inheritance.

Types of inheritance in java

On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.
In java programming, multiple and hybrid inheritance is supported through interface only. We will learn about interfaces later.
Note: Multiple Inheritance is not supported in java through class.

Single Inheritance:

In single inheritance, subclasses inherit the features of one superclass. In program below, the class Phone serves as a base class for the derived iPhone
            int memory=4;
            float screenSize=5.5; 
class iPhone extends Phone{ 
            String manufacturer = "Apple";
            public static void main(String args[]){ 
                        iPhone ip=new iPhone(); 
                        System.out.println("iPhone Memeory is: "+ip.memory);
                        System.out.println("iPhone Screen size is: "+ip.screenSize);
                        System.out.println("iPhone Manufacturer is: "+ip.manufacturer);
            }
}

Multilevel Inheritance:

In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class. In Program image, the class Phone serves as a base class for the derived class SmartPhone, which in turn serves as a base class for the derived class iPhone. In Java, a class cannot directly access the grandparent’s members.
class Phone{
            void call();
            void message();
class SmartPhone extends Phone{ 
            void toggleWifi();
            void toggleBluetooth();
}
class iPhone extends SmartPhone{
            void connectToMAC();
            void siri();
}

Hierarchical Inheritance:

In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one sub class. In below image, the class Phone serves as a base class for the derived class iPhone and AnroidPhone
class Phone{
            void call();
            void message();
class SmartPhone extends Phone{ 
            void call();
void message();
void toggleWifi();
            void toggleBluetooth();
}
class iPhone extends SmartPhone{
            void connectToMAC();
            void siri();
}
class AnroidPhone extends SmartPhone{
void connectToPC();
}

Why multiple inheritance is not supported in java?

To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
Consider a scenario where Java, Test and JavaTest are three classes. The JavaTest class inherits Java and Test classes. If Java and Test classes have same method and you call it from child class object, there will be ambiguity to call method of Java or Test class.
Since compile time errors are better than runtime errors, java renders compile time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error now.
class Java{
void print(){System.out.println("Java");} 
class Test{ 
void print(){System.out.println("Test");} 
class JavaTest extends Java,Test{       //suppose if it were
public Static void main(String args[]){
JavaTest obj=new JavaTest ();
obj.print();      //Now which print() method would be invoked?

December 17, 2017

Classes and Objects

Here in this chapter, we will look into the concepts of Classes and Objects.

Class:
  • Class is used to create our own data type
  • Class is a blue print which has only template
  • Class will describe the behavior and state of the objects
  • Class is a logical entity which will occupy any space in the memory
Syntax:
Class <className>
{
<datatype 1 > <variable 1>;
<datatype 2 > <variable 2>;
<datatype 3 > <variable 3>;

void method1(){}
void method2(){}
}
In the above syntax, variables/fields represent the states and methods represents the behavior

Ex:
Class Phone
{
int memory;
String manufacturer;
double screenSize;

void call(){}
void message(){}
}

In the above example Phone is a User defined data type. Memory, manufacturer and screenSize are the fields which represents the state of the Phone. Call and message are the two features of the phone which represents the behavior of the phone.
Objects:
  • Objects have the states and behavior
  • Objects will be created the class and the
  • Objects will have state and behavior
  • As the Objects are physical entities they will stored in the memory
How to create the Objects?
Syntax:
<Class Name> <variable Name> = new <Class Name>();
In the above Syntax, we have 3 steps
1.        Declaration - <Class Name> <variable Name>
2.       Instantiation – new keyword is used to create the objects
3.       Initialization – new keyword followed by <class name>()
Ex:
                Phone lPhone =new Phone();

The following program shows the implementation of CLASS & OBJECTS

Class Phone
{
// Fields/Class Variables Declaration
                int memory; //1,2,3,4
                String Manfacturer; //"Sansung", "Sony", "Apple", "Windows"
                float dimention; //5,5.5,4.5
               
// Behavior/Methods Declaration
                void call()
                {
                }
                void message()
                {
                }
               
                void tuggleWifi()
                {
                }

Public static void main(String args[])
{
// Object Creation
Phone IPhone=new Phone();
}
}