Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

-----------------------------------------------------------------------------------

-----------------------------------------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~INHERITANCE~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-----------------------------------------------------------------------------------
----------------------------------------------------------------------->> final
KEYWORD :

1. We can declare variable as a final, method as A final,


class as a final.
2. If we declare variable as a final , we can't change the
value of final variable.
3. If we declare any method as a final, we can't override
that method.
4. If we declare any class as a final , we can't perform or
we can't inherit that class.
5. we can also initialize a blank final variable, only
inside a static method.
6. We can't declare a constructor as a final.

-----------------------------------------------------------------------------------
----------------------------------------------------------------------
->> this KEYWORD :

1. It is used to fetch the current instance value of a


class.
2. We can use this method and we can also pass this as a
arguement in a method.

-----------------------------------------------------------------------------------
----------------------------------------------------------------------
->> static KEYWORD :

1. We can declare variable method and nested class as a


static.
2. We can't declare outer class as a static class.
3. In a static method we can't call or use this or super
KEYWORD.
4. We can't use non-static var inside static method.
5. We can't directy access non-static variable and non-
static mehtod inside a static class or a static method.
6. By creating a object of a class we can access non-static
variable and non-static mehtod inside a static class or static method.
ex :
class abc
{ int x=10;
void dis(){
sop(x)}
PSVM(---)
{
abc obj=new abc();
sop(obj.x);
obj.dis();
}
}

-----------------------------------------------------------------------------------
----------------------------------------------------------------------
>> In JAVA we can't implement multiple or herarichal inheritance , if you want
to implement the concept of multiple inheritance that can be possible only with a
interface.
Syntax :
class Parent{
}
class child extends Parent{
}
->> super KEYWORD :

1. It ios used to invoke parent class variable, method and


constructor.
2.
-----------------------------------------------------------------------------------
----------------------------------------------------------------------
>> When we cast child class into a parent class that process is known as upcasting.
>> When we cast parent class into a child class that process is known as
downcasting.
demotration :

1. class parent{
void sum()
{
SOP)"PArent MEthod call");
}
}
class child extends parent{
void print()
{
SOP("Child Method call");
// Example of Upcasting
}
}
class test
{
PSVM(______)
{
parent p=new child();
p.sum();
p.print(); //It will give error
}
}

2. To remove error

class test{
PSVM(_____)
{
parent p=new child();
p.sum();
// Example of Downcasting.
child c=(child)p;
c.sum();
c.print();
}
}

-----------------------------------------------------------------------------------
----------------------------------------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ABSTRACT
CLASS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-----------------------------------------------------------------------------------
----------------------------------------------------------------------

>> In java we can also perform abstraction by declaring a class or as abstract.


>> An abstract is a keyword that is used to make abstract class and abstract
method.
>> Abstract class is collection is a collection of abstract method and non-abstract
method.
>> Abstract method only declare inside a abstract class.
>> When we declare any method as a abstract method there is no need to define body
of method.
>> We can't create instance of abstract class.
>> We can't directly inherite the abstract class , so there are two ways for
performing the inheritance in abstarct class :

1. Either by making the child class as a abstract class.


2. Override all the abstract method into a child class.

** WAP for creating a abstract class named as shape, inside the class declare two
private variable named as 'width' and 'height', also declare one parameterized
constructor and one abstract method named as 'area'. Create 2 subclass square and
rectangle that inherite shape class.
in a subclasses use the same variable that is already decre inside the shape class.

-----------------------------------------------------------------------------------
----------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------------------------------------------------

Ques :-

Create an abstract class name as employee.


Declare 3 var as a private emp_name, emp_id, emp_mob.
create parameterized constructor for initializing value.
Create 1 abstract method , the method name is compute salary and it returns double
value, which is data type.
Create a another class name d as a manager that inherite all the properties of
employee class.
Declare 1 more parameter i.e. salary.
Override the compute salary method
Create a new method for displaying all the details of the manager.

You might also like