0% found this document useful (0 votes)
20 views

Unit I - Inheritance

The document discusses different types of variables in Java including local variables, instance variables, and static variables; local variables are declared within methods and blocks while instance variables are declared within a class but outside methods and static variables are declared similarly but with the static keyword; it provides details on how each type of variable is declared, initialized, accessed, and destroyed.

Uploaded by

Srinivasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Unit I - Inheritance

The document discusses different types of variables in Java including local variables, instance variables, and static variables; local variables are declared within methods and blocks while instance variables are declared within a class but outside methods and static variables are declared similarly but with the static keyword; it provides details on how each type of variable is declared, initialized, accessed, and destroyed.

Uploaded by

Srinivasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

UNIT I - INHERITANCE

•Inheritance can be defined as the process


where one class acquires the properties
(methods and fields) of another.
•Information is made manageable in a
hierarchical order.
•extends Keyword
•keyword used to inherit the properties of a
class
UNIT I - INHERITANCE
• Syntax:
• class Super
{
..... .....
}
class Sub extends Super
{
..... .....
}
UNIT I - INHERITANCE
class Calculation
{
int z;
public void addition(int x, int y) {
z = x + y;
System.out.println("The sum of the given numbers:"+z);
}
public void Subtraction(int x, int y) {
z = x - y;
System.out.println("The difference between the given
numbers:"+z);
}
}
UNIT I - INHERITANCE
public class My_Calculation extends Calculation
{
public void multiplication(int x, int y) {
z = x * y;
System.out.println("The product of the given numbers:"+z);
}
public static void main(String args[]) {
int a = 20, b = 10;
My_Calculation demo = new My_Calculation(); demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
}
}
UNIT I - INHERITANCE
• Output:
• javac My_Calculation.java
• java My_Calculation
• The sum of the given numbers:30
• The difference between the given numbers:10
• The product of the given numbers:200
UNIT I - ENCAPSULATION
• /* File name : EncapTest.java */
• public class EncapTest {
• private String name;
• private String idNum;
• private int age;

• public int getAge() {


• return age;
• }

• public String getName() {


• return name;
• }
UNIT I - ENCAPSULATION
public String getIdNum() {
return idNum;
}

public void setAge( int newAge) {


age = newAge;
}

public void setName(String newName) {


name = newName;
}

public void setIdNum( String newId) {


idNum = newId;
}
}
UNIT I - ENCAPSULATION
/* File name : RunEncap.java */
public class RunEncap {
public static void main(String args[]) {
EncapTest encap = new EncapTest();
encap.setName("James");
encap.setAge(20);
encap.setIdNum("12343ms");
System.out.print("Name : " + encap.getName() + " Age : " +
encap.getAge());
}
}
Output:
Name : James Age : 20
UNIT I - POLYMORPHISM
/* File name : Employee.java */
public class Employee {
private String name;
private String address;
private int number;
public Employee(String name, String address, int number) {
System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
}
public void mailCheck() {
System.out.println("Mailing a check to " + this.name + " " + this.address);
}
UNIT I - POLYMORPHISM
public String toString() {
return name + " " + address + " " + number;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public void setAddress(String newAddress) {
address = newAddress;
}
public int getNumber() {
return number;
}
}
UNIT I - POLYMORPHISM
/* File name : Salary.java */
public class Salary extends Employee {
private double salary; // Annual salary

public Salary(String name, String address, int number, double salary) {


super(name, address, number);
setSalary(salary);
}

public void mailCheck() {


System.out.println("Within mailCheck of Salary class ");
System.out.println("Mailing check to " + getName()
+ " with salary " + salary);
}
UNIT I - POLYMORPHISM

public double getSalary() {


return salary;
}
public void setSalary(double newSalary) {
if(newSalary >= 0.0) {
salary = newSalary;
}
}
public double computePay() {
System.out.println("Computing salary pay for " +
getName());
return salary/52;
}
}
UNIT I - POLYMORPHISM
/* File name : VirtualDemo.java */
public class VirtualDemo {
public static void main(String [] args) {
Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP",
3, 3600.00);
Employee e = new Salary("John Adams", "Boston, MA", 2,
2400.00);
System.out.println("Call mailCheck using Salary reference
--");
s.mailCheck();
System.out.println("\n Call mailCheck using Employee
reference--");
e.mailCheck();
}}
UNIT I - POLYMORPHISM
UNIT I – LOCAL VARIABLES
• Local Variables
• Local variables are declared in methods, constructors, or blocks.
• Local variables are created when the method, constructor or
block is entered and the variable will be destroyed once it exits
the method, constructor, or block.
• Access modifiers cannot be used for local variables.
• Local variables are visible only within the declared method,
constructor, or block.
• Local variables are implemented at stack level internally.
• There is no default value for local variables, so local variables
should be declared and an initial value should be assigned
before the first use.
UNIT I – LOCAL VARIABLES
public class Test {
public void pupAge() {
int age = 0;
age = age + 7;
System.out.println("Puppy age is : " + age);
}

public static void main(String args[]) {


Test test = new Test();
test.pupAge();
}
}
Output: Puppy age is: 7
UNIT I – INSTANCE VARIABLES
• Instance variables are declared in a class, but outside a method, constructor or any block.
• When a space is allocated for an object in the heap, a slot for each instance variable value is
created.
• Instance variables are created when an object is created with the use of the keyword 'new' and
destroyed when the object is destroyed.
• Instance variables hold values that must be referenced by more than one method, constructor or
block, or essential parts of an object's state that must be present throughout the class.
• Instance variables can be declared in class level before or after use.
• Access modifiers can be given for instance variables.
• The instance variables are visible for all methods, constructors and block in the class. Normally, it
is recommended to make these variables private (access level). However, visibility for subclasses
can be given for these variables with the use of access modifiers.
• Instance variables have default values. For numbers, the default value is 0, for Booleans it is false,
and for object references it is null. Values can be assigned during the declaration or within the
constructor.
• Instance variables can be accessed directly by calling the variable name inside the class. However,
within static methods (when instance variables are given accessibility), they should be called using
the fully qualified name. ObjectReference.VariableName.
UNIT I – INSTANCE VARIABLES

import java.io.*;
public class Employee1 {

// this instance variable is visible for any child class.


public String name;

// salary variable is visible in Employee1 class only.


private double salary;

// The name variable is assigned in the constructor.


public Employee1 (String empName) {
name = empName;
}

// The salary variable is assigned a value.


public void setSalary(double empSal) {
salary = empSal;
}
UNIT I – INSTANCE VARIABLES
// This method prints the employee details.
public void printEmp() {
System.out.println("name : " + name );
System.out.println("salary :" + salary);
}
public static void main(String args[]) {
Employee1 empOne = new Employee1("Ransika");
empOne.setSalary(1000);
empOne.printEmp();
}
}
Output:
name : Ransika
salary :1000.0
UNIT I – STATIC VARIABLES
• Class variables also known as static variables are declared with the static keyword in a class,
but outside a method, constructor or a block.
• There would only be one copy of each class variable per class, regardless of how many objects
are created from it.
• Static variables are rarely used other than being declared as constants. Constants are variables
that are declared as public/private, final, and static. Constant variables never change from their
initial value.
• Static variables are stored in the static memory. It is rare to use static variables other than
declared final and used as either public or private constants.
• Static variables are created when the program starts and destroyed when the program stops.
• Visibility is similar to instance variables. However, most static variables are declared public
since they must be available for users of the class.
• Default values are same as instance variables. For numbers, the default value is 0; for
Booleans, it is false; and for object references, it is null. Values can be assigned during the
declaration or within the constructor. Additionally, values can be assigned in special static
initializer blocks.
• Static variables can be accessed by calling with the class name ClassName.VariableName.
• When declaring class variables as public static final, then variable names (constants) are all in
upper case. If the static variables are not public and final, the naming syntax is the same as
instance and local variables.
UNIT I – STATIC VARIABLES

You might also like