Java Unit 2 new
Java Unit 2 new
Inheritance
Inheritance means Reusability. It is very useful.
The capability of a class to inherit the properties of some other class is
known as Inheritance.
When we create a new class from the old class, it’s called Inheritance.
The capability of a class to inherit properties from another class is called
Inheritance.
It’s one of the important fundamentals of OOP principles.
Inheritance represents the IS-A relationship which is also known as
a parent-child relationship.
Advantages of Inheritance
o Reusability.
o It saves Memory Space.
o It saves Time.
o Increase Reliability of Code.
o Saves Developing and Testing efforts and Money too.
o Fast Process.
PREPARED BY: PATEL MANESH - M.Sc(CA & IT) Contact: 90165 17796 1
PATEL MANESH
Example
The extends keyword indicates that you are making a new class that
derives from an existing class.
The meaning of "extends" is to increase the functionality.
Types of inheritance in java
There can be three types of inheritance in java: single, multilevel and
hierarchical.
JAVA does not support multiple and hybrid inheritances.
Both are supported by interface only. We will learn about interfaces later.
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 2
PATEL MANESH
class A
{
int no;
String name;
void get()
{
no = 10;
name = “Manish”;
}
}
class B extends A
{
void display()
{
System.out.println (no);
System.out.println (name);
}
}
class Demo
{
public static void main (String M[])
{
B b=new B();
b.get();
b.display(); 10
} Manish
}
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 3
PATEL MANESH
class C extends B
{
void display()
{
System.out.println (no1);
System.out.println (no2);
System.out.println (name);
}
}
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 4
PATEL MANESH
Hierarchical Inheritance
If there is only one super class and more than one child class in a program,
it’s called Hierarchical Inheritance.
A single base class can have multiple derived classes, and other subclasses
can further inherit these derived classes, forming a hierarchy of classes.
class A class Demo
{ {
int no1,no2; public static void main (String M[])
{
void get() B obj1 = new B();
{
C obj2 = new C();
no1 = 200;
no2 = 200;
}
obj1.get();
}
obj1.add();
class B extends A
{
int sum; obj2.get();
void add() obj2.mul();
{
sum = no1 + no2; }
System.out.println (sum); }
}
}
class C extends A
{
int res;
void mul()
{
res = no1 * no2;
System.out.println (res);
}
}
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 5
PATEL MANESH
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 6
PATEL MANESH
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 7
PATEL MANESH
class Animal
{
public void move()
{
System.out.println ("Animals can move");
}
}
class Dog extends Animal
{
public void move()
{
System.out.println ("Dogs can walk and run");
}
}
class Demo
{
public static void main (String M[])
{
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 8
PATEL MANESH
class Parent
{
class Order
{
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 9
PATEL MANESH
} White
}
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 10
PATEL MANESH
class Test
{
public static void main(String M[])
{
Child C=new Child();
C.Display();
}
}
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 11
PATEL MANESH
class Parent
{
class Order
{
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 12
PATEL MANESH
1. static method
2. private access modifier
3. final class
4. final method
1. static method cannot overriding
class B extends A }
{ }
void get()
{
System.out.println ("Sub class");
}
}
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 13
PATEL MANESH
final class A
{
A()
{
System.out.println ("Super class");
}
}
class B extends A
{
B()
{
System.out.println ("Sub class");
}
}
class Demo5
{
public static void main (String M[])
{
B b=new B();
}
}
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 14
PATEL MANESH
class A
{
final void display()
{
System.out.println ("Super class");
}
}
class B extends A
{
void display()
{
System.out.println ("Sub class");
}
}
class Demo5
{
public static void main (String M[])
{
B b=new B();
b.display();
}
}
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 15
PATEL MANESH
Points to Remember
o An abstract class must be declared with an abstract keyword.
o It can have abstract and non-abstract methods.
o It cannot be instantiated.
o It can have constructors and static methods also.
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 16
PATEL MANESH
{
System.out.println ("Abstract method");
}
}
}
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 17
PATEL MANESH
class Test
{
public static void main (String M[])
{
Shape s=new Circle(); //Reference
s.show();
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 18
PATEL MANESH
Interface in Java
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 19
PATEL MANESH
Syntax:
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 20
PATEL MANESH
interface Demo
{
void print();
}
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 21
PATEL MANESH
interface ABC
{
void print();
}
interface XYZ
{
void show();
}
class Manish implements ABC, XYZ
{
public void print()
{
System.out.println ("Hello");
}
public void show()
{
System.out.println ("Welcome");
}
public static void main (String M[])
{
Manish m = new Manish();
m.print();
m.show();
Hello
} Welcome
}
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 22
PATEL MANESH
Interface inheritance
interface ABC
{
void print();
}
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 23
PATEL MANESH
I am from Interface
I am from class
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 24
PATEL MANESH
Abstract class and interface both are used to achieve abstraction where we
But there are many differences between abstract class and interface that are
given below.
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 25
PATEL MANESH
9)Example: Example:
abstract class Demo interface Demo
{ {
public abstract void draw(); void paint();
} }
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 26
PATEL MANESH
Package
A java package is a group of similar types of classes, interfaces and sub-
packages.
Package in java can be categorized in two form, built-in package and user-
defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net,
io, util, sql etc.
Here, we will have the detailed learning of creating and using user-defined
packages.
Packages are used in Java, in-order to avoid name conflicts and to control
access of class, interface and enumeration etc.
Using package it becomes easier to locate the related classes.
It also provides a good structure for projects with hundreds of classes and
other files.
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 27
PATEL MANESH
Creating a package
Creating a package in java is quite easy.
Simply include a package command followed by name of the package as
the first statement in java source file.
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 28
PATEL MANESH
package Manesh;
class Demo
{
public static void main(String M[])
{
System.out.println ("Welcome to package");
}
}
To Compile:
javac -d . Demo.java OR javac -d d:/Jay Demo.java
To Run:
import keyword
import keyword is used to import built-in and user-defined packages into
your java source file
So that your class can refer to a class that is in another package by directly
using its name.
There are 3 different ways to refer to any class that is present in a different
package:
If you use fully qualified name to import any class into your program,
then only that particular class of the package will be accessible in
your program, other classes in the same package will not be
accessible.
For this approach, there is no need to use the import statement.
But you will have to use the fully qualified name every time you are
accessing the class or the interface, which can look a little untidy if
the package name is long.
This is generally used when two packages have classes with same
names.
For example: java.util and java.sql packages contain Date
class.
package sem1; package sem2;
public class A class B
{ {
public void msg() public static void main(String M[])
{ {
System.out.println ("Hello"); //using fully qualified name
} sem1.A obj = new sem1.A();
} obj.msg();
//save by A.java }
} //save by B.java
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 30
PATEL MANESH
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 31
PATEL MANESH
Point to remember
While creating a package, care should be taken that the statement for
creating package must be written before any other import statements.
// not allowed
import package sem2.*;
import java.io.*;
package sem4;
//correct syntax
package sem4;
import package p1.*;
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 32
PATEL MANESH
Java.lang package
java.lang.*; Provides classes that are fundamental to the design of the Java
programming language.
The most important classes are Object, which is the root of the class
hierarchy, and Class, instances of which represent classes at run time.
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 33
PATEL MANESH
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 34
PATEL MANESH
The Object class provides some common methods to all the objects such as
object can be compared, object can be cloned, object can be notified etc.
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 35
PATEL MANESH
1. GetClass() method
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 36
PATEL MANESH
t = null;
System.out.println("end");
}
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 37
PATEL MANESH
Demo ()
{
rollno=10;
name="manish";
}
System.out.println(s1.rollno+" "+s1.name);
System.out.println(s2.rollno+" "+s2.name);
}
catch(CloneNotSupportedException c){}
}
}
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 38