Monday 27 August 2018

Abstract Methods and Classes


Abstract Methods and Classes

Whenever a class contains a method only for the purpose of overriding but does not have any body such methods has to be declare as Abstract Methods. Once a class contains at least one abstract method the class must be declared as abstract and for an abstract class we cannot create any instances but it can be inherited. Every method in the abstract class has to be override.
Ex:      abstract class common
            {
                        abstract void assign();
                        abstract void show();
            }         


Final Variables, Methods and Classes


Final Variables, Methods and Classes

 The keyword "final" in Java is used in different ways depending on the context. We can have final methods, final classes, final data members, final local variables and final parameters. A final class implicitly has all the methods as final, but not necessarily the data members. A final class may not be extended, neither may a final method be overridden.

It is applicable for a class for the following 3 purposes:-
Ø  For the members
Ø  For the methods
Ø  For the class itself.

§  If it is applied for members they become sharable constants and can be used in other classes but it cannot be modified

§  If it is applied for methods the methods cannot be overridden in child classes.
§  If it is applied for the class itself it cannot be inherited but instances can be created.
class tree          //if you keep final before the class its not inheritable further
{
            final String head;         //it becomes readonly
            public tree()
            {
                        head="Final Keyword Concept";
            }
            final void assign() //cannot be overridden in subclass
            {
                        //head="Final"; not valid
            }
}
class node extends tree           
{
            /* void assign()
            {
                        head="Child node";
            }          */
}
class impfinal
{
            public static void main(String []args)
            {
                        node n=new node();
                        n.assign();
                        System.out.println(n.head);
            }
}
Output:
C:\>javac impfinal.java
C:\>java impfinal
Final Keyword Concept

Inheritance


Inheritance
The mechanism of deriving a new class from an old one is called as INHERITANCE. The existing classes are called as base/parent classes where as the new classes are called as child/derived classes. The derived class inherits all the capabilities of the base class and can add refinements and extensions of its own.


Defining derived classes
A derived class can be defined by specifying its relationship with the base class in addition to its own details. The general form of defining a derived class is:
class subclassname extends superclassname
{
            variable declaration;
            method declaration;
}

Java supports the following types of Inheritance
a)         Single
b)        Multilevel
c)         Hierarchical

Single inheritance: Derivation of a class from only one base class is called single inheritance.

class Dept
{
            int dno;
            String dname;
            String loc;
            void setDetails(int x,String y,String z)
            {
                        dno=x;
                        dname=y;
                        loc=z;
            }
}
class emp extends Dept
{
            int eno;
            String ename;
            String job;
            void setemp(int x,String y,String z)
            {
                        eno=x;
                        ename=y;
                        job=z;
            }
            void showdetails()
            {
                        System.out.println("Dept Details="+dno+"\t\t"+dname+"\t\t"+loc);
                        System.out.println("Emp Details="+eno+"\t\t"+ename+"\t\t"+job);
            }
}
class single
{
            public static void main(String []args)
            {
                        emp e=new emp();
                        e.setDetails(10,"sales","vizag");
                        e.setemp(101,"Smith","manager");
                        e.showdetails();
            }
}
Output:
C:\>javac single.java
C:\>java single
Dept Details=10         sales           vizag
Emp Details=101         Smith           manager

Hierarchical inheritance: Derivation of several classes from a single base class i.e. the traits of one class may be inherited by more than one class is called hierarchical inheritance. We can achieve runtime polymorphism with the help of method overriding.

Example:

class common
{
            void assign() { }
            void show( ) { }
}
class prodinfo extends common
{
            int pno;
            String pname;
            public void assign()
            {
                        pno=100;
                        pname="Lux";
            }
            public void show()
            {
                        System.out.println("Product Details");
                        System.out.println(pno+"\t\t"+pname);
            }
}
class custinfo extends common
{
            int cno;
            String cname;
            public void assign()
            {
                        cno=10;
                        cname="Gayatri";
            }
            public void show()
            {
                        System.out.println("Customer Details");
                        System.out.println(cno+"\t\t"+cname);
            }
}
class hierar
{
            public static void main(String []args)
            {
                        common c;       //reference variable
                        if(args[0].equals("product"))
                        c=new prodinfo();
                        else
                        c=new custinfo();
                        c.assign();
                        c.show();
            }
}
Output:
C:\>javac hierar.java
C:\>java hierar opo
Customer Details
10              Gayatri
C:\>java hierar product
Product Details
100             Lux

Multilevel inheritance: Derivation of a class from another derived class is called multilevel inheritance.


Example:
class student
{
int rno;
void get_no(int a)
{
rno=a;
}
void put_no()
{
System.out.println("Rno="+rno);
}
}
class test extends student
{
float sub1,sub2;
void get_marks(float sub1, float sub2)
{
this.sub1=sub1;
this.sub2=sub2;
}
void put_marks()
{
System.out.println("Sub1="+sub1);
System.out.println("Sub2="+sub2);
}
}
class result extends test
{
float total;
void display()
{
total=sub1+sub2;
System.out.println("Total="+total);
}
}
class multi
{
            public static void main(String []args)
            {
                        result r=new result();
                        r.get_no(9);
                        r.get_marks(56.7f,78.9f);
                        r.put_no();
                        r.put_marks();
                        r.display();
            }
}
Output:
C:\>javac multi.java
C:\>java multi
Rno=9
Sub1=56.7
Sub2=78.9
Total=135.6