Q: What is the difference between default constructor and parameterized constructor?
2) Narrowing: Converting a higher data type into lower type is called Narrowing
Ex: int n = 65;
Char ch = (char) n;
Abstract class:
A method consists of two parts: method header and method body. Method header represents the future the programmer wants in a class. Method body represents how to implements this future. Implementing means writing body for method. When a method has got different implementation in different objects than the programmer cannot write method body in the super class.A method without method body is called a abstract method. A class that contains abstract method is called abstract class. To declare abstract method class should be declare using keyword abstract. Concrete methods mean complete methods. We cannot create object to abstract class.
Points:
1) An abstract class is a class with zero or more abstract methods.
2) An abstract class can have abstract method, concrete method and instance variables.
3) Both the abstract class and abstract methods should be declared as abstract.
4) We cannot create an object to abstract class.
5) We can create reference variables of abstract class type.
6) All the abstract methods of abstract class should be implemented in its subclasses.
7) If any method is not implemented that subclass should also be declare as abstract.
8) Abstract class reference variable can be used to refer to the objects.
9) Abstract class reference cannot refer to individual method of a class.
10) A class cannot have both abstract and final.
Example:
abstract class Car {
int regno;
Car(int regno) {
this.regno=regno;
}
void fillTank()//concrete method {
System.out.println("Fill Tank");
}
abstract void steering(int direction);
abstract void breaking(int force);
}
class Maruti extends Car {
Maruti(int regno) {
super(regno);
}
void steering(int direction) {
System.out.println("Regno of Maruti="+regno);
System.out.println("Maruti uses manual steering="+direction);
System.out.println("please drive the maruti car");
}
void breaking(int force) {
System.out.println("Breaking of Maruti="+force);
System.out.println("Maruti uses hydralic breaks");
System.out.println("Apply breaks stop the car");
} }
class Santro extends Car {
Santro(int regno) {
super(regno);
}
void steering(int direction) {
System.out.println("Regno of Santro="+regno);
System.out.println("Santro uses manual steering="+direction);
System.out.println("please drive the Santro car");
}
void breaking(int force) {
System.out.println("Breaking of Santro="+force);
System.out.println("Santro uses hydralic breaks");
System.out.println("Apply breaks stop the car");
} }
class AbstractDemo {
public static void main(String[] args) {
Maruti m=new Maruti(6666);
Santro s=new Santro(9999);
Car c,c1;
c=m;
c1=s;
c.fillTank();
c.steering(2);
c.breaking(200);
c1.fillTank();
c1.steering(2);
c1.breaking(200);
} }
Interface:
An interface is a specification of method prototype. All the methods of interface are by default abstract only. We cannot create an object to interface.Points:
1) Interface is a specification of method prototype or method header.
2) An interface contains zero or more abstract methods.
3) All the methods of the interface are public and abstract by default.
4) An interface can also contain variables which are public, static and final by default.
5) When an interface is written any third party vendor can implement it.
6) All the abstract method of the interface should be implemented in a class called implementation class.
7) If any method is not implemented than that class should be declare as abstract.
8) We cannot create an object to an interface.
9) We can create a reference of an interface.
10) Interface reference can be used to refer to the objects of its implementation classes.
11) We can write a class within an interface.
12) An interface cannot implement another interface.
13) An interface can extends another interface.
14) A single class can implement multiple interfaces.
Example1:
import java.io.*;
interface MyInter {
void connect();
}
class OracleDb implements MyInter {
public void connect() {
System.out.println("Connecting to oracle");
} }
class SybaseDb implements MyInter {
public void connect() {
System.out.println("Connecting to sybase");
} }
class Inter {
public static void main(String[] args) throws Exception {
/*Class c=Class.forName(args[0]);
MyInter mi=(MyInter)c.newInstance();
mi.connect();*/
OracleDb ob=new OracleDb();
SybaseDb sb=new SybaseDb();
MyInter m1,m2;
m1=ob;
m1.connect();
m2=sb;
m2.connect();
} }
Example 2:
//An Interface can extends more than one interface,but one interface can't implements another interface
interface First {
void disp1();
}
interface Second {
void disp2();
}
interface Third extends First,Second {
void disp3();
}
class Test implements Third {
public void disp3() {
System.out.println("disp3()");
}
public void disp1() {
System.out.println("disp1()");
}
public void disp2() {
System.out.println("disp2()");
} }
class Inter1 {
public static void main(String[] args) {
Test t=new Test();
t.disp1();
t.disp2();
t.disp3();
//System.out.println("Hello World!");
} }
Example 3:
//class inside interface interface Outer
{
void disp();
class Inner {
void test()
{
System.out.println("Inner");
} }
}
class Main implements Outer {
public void disp() {
System.out.println("disp");
} }
class Inter2 {
public static void main(String[] args) {
Main m=new Main();
m.disp();
Outer.Inner i=new Outer.Inner();
i.test();
System.out.println("Hello World!");
} }
Multiple Inheritance: Java doesn’t support multiple inheritance. We can achieve multiple inheritance by using multiple interfaces.
Example:
//Multiple inheritance by using multiple interface interface Father
{
int PROP1=50000;
float HT1=6.2F;
}
interface Mother {
int PROP2=20000;
float HT2=5.4F;
}
class Child implements Father,Mother {
void property() {
System.out.println("Child property="+(PROP1+PROP2));
}
void height()
{
System.out.println("Child height="+(HT1+HT2)/2);
} }
class Multi {
public static void main(String[] args) {
Child c=new Child();
c.property();
c.height();
} }
Packages:
A packages represents a sub directory that contains a group of related classes and interfaces. Packages hide classes and interfaces in separate subdirectories. Accidental deletion is not possible. The classes of one package are different from the classes of another package. Packages provide the reusability of code. We can create our own package as well as extend already available packages.Example 1 Addition.java package ddd;
public class Addition {
double d1,d2;
public Addition(double d1,double d2) {
this.d1=d1;
this.d2=d2;
}
public void sum() {
System.out.println("sum="+(d1+d2));
} }
Note: > javac –d . Addition.java UseAdd.java
import ddd.Addition;
class UseAdd {
public static void main(String[] args) {
Addition a=new Addition(24,534);
Addition a1=new Addition(25,53);
//ddd.Addition a=new ddd.Addition(25,55);
a.sum();
a1.sum();
} }
Example 2:
MyDate.java package cdf;
public interface MyDate {
void showDate();
}
MyDateImpl.java package cdf;
import java.util.Date;
public class MyDateImpl implements MyDate {
public void showDate() {
Date d=new Date();
System.out.println(d);
} }
Useinterface.java
import cdf.MyDateImpl;
class Useinterface {
public static void main(String[] args) {
MyDateImpl m=new MyDateImpl();
m.showDate();
} }
SubPackage Example Sample.java
package demo.aptech;
public class Sample
{
public void show() {
System.out.println("sub packages");
} }
UseSample.java
import demo.aptech.Sample;
class UseSample {
public static void main(String[] args) {
Sample s=new Sample();
s.show();
} }