Java Notes6 PDF
Java Notes6 PDF
Subpackage Description
java.lang Contains a large number of general-purpose classes
java.io Contains I/O classes
java.net Contains classes that support networking
java.lang contains various classes like System. java.lang package is unique because it
is imported automatically into every Java program. So, we need not explicitly import
java.lang package.
Prepared By – Asmita Ranade
Abstraction
In OOP, Abstraction is defined as hiding the unnecessary details from the user and
to focus on essential details. In Java, Abstraction can be achieved using
‘Abstract’ classes and methods.
A class in Java which is declared with ‘abstract’ keyword is an Abstract class. It
may have both abstract and non-abstract methods. We need to extend the abstract
class and implement its methods. It cannot be instantiated.
Syntax – abstract class classname
{ //abstract or non-abstract methods
}
Prepared By – Asmita Ranade
Abstract Methods
A method declared using the ‘abstract’ keyword within an abstract class and does
not have a definition is called an abstract method.
Abstract method does not have a method body. It may have zero or more
arguments.
Syntax – abstract returntype methodname ( [args list] );
In superclass, an abstract method can be declared which is then implemented in
subclass.
If a non-abstract class extends an abstract class, then the class must implement all
the abstract methods of that abstract class. If not, then the class should be declared
as abstract as well. Prepared By – Asmita Ranade
Abstract Class and Method Example
abstract class SumNumbers
{ public abstract int SumTwo(int n1, int n2); Abstract Methods
public abstract int SumThree(int n1, int n2, int n3);
public void display() Non-abstract method
{ System.out.println("Method of abstract class SumNumbers");
}
}
class DemoAbstract extends SumNumbers
{ public int SumTwo(int num1, int num2)
{ return (num1+num2);
}
public int SumThree(int num1, int num2, int num3)
{ return (num1+num2+num3);
}
Imp Statement
public static void main(String args[])
{ SumNumbers s = new DemoAbstract();
System.out.println("Sum of 2 numbers 10 and 56 = " + s.SumTwo(10,56));
System.out.println("Sum of 3 numbers 11, 45 and 63 = " + s.SumThree(11,45,63));
s.display();
} Prepared By – Asmita Ranade
}
Abstract Class and Method Example
abstract class Shape Abstract Class & class DemoShape
{ abstract void area(); Method { public static void main(String args[])
} {
class Rectangle extends Shape Shape sh= new Rectangle(12,15); Imp Statements
{ int l,b; sh.area();
Rectangle(int len, int brd) sh = new Square(5);
{l=len; sh.area();
b=brd; }
} }
public void area()
{ System.out.println("Rectangle Area = " + (l*b));
}
}
class Square extends Shape
{ int s;
Square(int side)
{ s=side;
}
public void area()
{ System.out.println("Square Area = " + (s*s));
}
} Prepared By – Asmita Ranade
Interfaces
An interface is a collection of abstract methods. It is syntactically similar to
classes but it lacks instance variables.
Once an interface is defined, any number of classes can implement an interface.
Also, one class can implement any number of interfaces.
To implement an interface, a class must create the complete set of methods defined
by the interface.
In an interface,
Syntax – access interface interfacename methods are implicitly
public. Variables
{ returntype method1(parameter list); declared in an interface
returntype method2 (parameter list); are implicitly public,
final, static and must be
type variable1 = value; initialised.
}
Prepared By – Asmita Ranade
Implementing Interfaces
To implement an interface, include the ‘implements’ keyword in class definition.
Syntax – class classname implements interfacename
{ class body
}