0% found this document useful (0 votes)
2 views16 pages

Week4-OOPS Intro, Constructors, Inheritence

The document explains the concepts of static and non-static members in Java, detailing how static members are accessed via the class name while non-static members require object instantiation. It also covers initialization blocks, constructors (default, zero-argument, and parameterized), and inheritance types, emphasizing rules and uses of inheritance. Additionally, it introduces composition as a relationship between classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views16 pages

Week4-OOPS Intro, Constructors, Inheritence

The document explains the concepts of static and non-static members in Java, detailing how static members are accessed via the class name while non-static members require object instantiation. It also covers initialization blocks, constructors (default, zero-argument, and parameterized), and inheritance types, emphasizing rules and uses of inheritance. Additionally, it introduces composition as a relationship between classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

STATIC AND NON-STATIC MEMBERS

Member: Any Variable or Method that is created directly in the class body is called as a
member.

STATIC MEMBERS
1. Static members are created using the “static” Keyword.
2. Static members are accessed using “Class name”.
Example 1:
class Demo
{
static int a = 25;
static void test()
{
System.out.println("Executing Test");
}
}
public class Program1
{
public static void main(String[] args)
{
System.out.println(Demo.a);
Demo.test();
}
}

Example 2:
class Sample
{
static int num = 15;
static double val = 4.5;
static void play()
{
System.out.println("Executing Play");
}
static void disp()
{
System.out.println("Executing disp");
}
}
public class Program2
{
public static void main(String[] args) {
System.out.println(Sample.num);
System.out.println(Sample.val);
Sample.play();
Sample.disp();
}
}
Example 3:

class Example
{
static char ch = 'U';
static int x = 90;
static void send()
{
System.out.println("Executing Send");
}
static void run()
{
System.out.println("Executing run");
}
}
public class Program3
{
public static void main(String[] args)
{
System.out.println(Example.ch);
System.out.println(Example.x);
Example.send();
Example.run();
}
}

NON-STATIC MEMBERS

1. Non-Static members are created without using “static” Keyword.


2. Non-Static members are accessed by creating an object.

Syntax for creating Object


classname variable = new classname();

Example 1:
class Delta
{
int a = 10;
void test()
{
System.out.println("Executing Test");
}
}
public class Program1
{
public static void main(String[] args)
{
Delta ref = new Delta();
System.out.println(ref.a);
ref.test();
}
}
CLASS & OBJECT
Class: An class is a template or blueprint used to define the properties of an object.
Object: An object represents a real world entity and has its own state and behaviour.

BLOCKS
STATIC INITALIZATION BLOCKS
1. These blocks are created using “Static” Keyword.
2. Syntax:
static
{
//initialization code here
}
3. Static blocks are used to initialize static data members only.

NON-STATIC INITIALIZATION BLOCKS

1. These blocks are created without using “static” keyword.


2. Example
{
//Initialization code here
}
3. Non static blocks are used to initialize non static data members only.
4. We can also create multiple non static blocks in a class they are executed sequentially.
5. Non static blocks are executed by the JVM at the time of object creation.
6. Non static blocks execute each time we create an object.

Example 1:
class Program1
{
int a;
{
a=10;
}
public static void main(String[] args)
{
System.out.println("Program Starts");
Program1 pgm = new Program1();
System.out.println(pgm.a);
System.out.println("Program Ends");
}
}

Example 2:
class Sample
{
int a;
int b;
{
a=10;
b=20;
}
public static void main(String[] args)
{
System.out.println("Program Starts");
Sample obj = new Sample();
System.out.println(obj.a);
System.out.println(obj.b);
System.out.println("Program Ends");
}
}

Example 3:
class Example
{
int num;
{
num=30;
}
{
num=60;
}
public static void main(String[] args)
{
System.out.println("Program Starts");
Example ex =new Example();
System.out.println(ex.num);
System.out.println("Program Ends");
}
}

Example 4:
class Delta
{
char ch;
{
System.out.println("Executing non-static block");
ch='Q';
}
public static void main(String[] args)
{
System.out.println("Program Starts");
Delta d = new Delta();
System.out.println(d.ch);
System.out.println("Program Ends");
}
}
CONSTRUCTOR

1. Constructors are special members or set of Instructions in a class that are used for
initialization (Assigning) and Instantiation (Object Creation).
2. Constructors have same name as class name.
3. Every class in a java has a constructor.
4. In a class Constructor can be created by a compiler or by the programmer.
5. The constructor will not have a return type.
6. We can Initialize the non-static data member in a class using the constructor.
7. Constructor will get executed at the time of object creation.
Syntax:
AccessSpecifier classname(Optional arguments)
{
//set of Instructions
}
8. Types of Constructors
a. Default Constructor
b. Zero Argument Constructor
c. Parameterized Constructor

DEFAULT CONSTRUCTOR

1. This constructor is created by the compiler.


2. The compiler creates this constructor when the programmer fails to create a constructor.
3. We cannot initialize values with the default constructor.
4. The default constructor provides default values to the data members.
Example 1:
class Demo
{
int a;
double val;
boolean bool;
}
class Program1
{
public static void main(String[] args)
{
System.out.println("Program Starts");
Demo d = new Demo();
System.out.println(d.a);
System.out.println(d.val);
System.out.println(d.bool);
System.out.println("Program Ends");
}
}

Example 2:
class Sample
{
int val=10;
void test()
{
System.out.println("Executing test");
}
}
class Program2
{
public static void main(String[] args)
{
Sample s = new Sample();
System.out.println(s.val);
s.test();
}
}

ZERO ARGUMENT CONSTRUCTOR

1. This constructor is created by the programmer.


2. As the name suggests the zero argument constructor does not accept arguments.
3. We can initialize non static data members in the body of the zero argument constructor.
4. All object created with zero arguments constructor will have same initialization.
Example 1:
class Alpha
{
int a=15;
void test()
{
System.out.println("Executing Test");
}
Alpha()
{
System.out.println("Constructor_ _ _");
}
}
public class Program1
{
public static void main(String[] args)
{
Alpha ref = new Alpha();
System.out.println(ref.a);
ref.test();
}
}
Example 2:
class Beta
{
int a;
double d;
boolean b;
Beta()
{
a=25;
d=45;
b=true;
}
}
class Program2
{
public static void main(String[] args)
{
Beta obj = new Beta();
System.out.println(obj.a);
System.out.println(obj.d);
System.out.println(obj.b);
}
}
PARAMETERIZED CONSTRUCTOR

1. This constructor is created by the programmer.


2. We can pass arguments to the parameterized constructor.
3. We can initialize non-static data members with the help of arguments in parameterized
constructors.
4. We can create objects with different initializations in case of a parameterized
constructor.

class Container
{
int capacity;
Container(int a)
{
capacity = a;
}
}
class Program2
{
public static void main(String[] args)
{
Container c1 = new Container(10);
System.out.println(c1.capacity);
Container c2 = new Container(100);
System.out.println(c2.capacity);
Container c3 = new Container(1000);
System.out.println(c3.capacity);
}
}
INHERITANCE
• Inheritance is the concept of one class acquiring the properties of another class.
• Inheritance is done in java using extends keyword.
• Inheritance is also known as [IS-A Relationship].
class A
{
Super/ Base/Parent
}
class B
{
Sub/Derived/Child
}

Example 1:
Example 2:

RULES O INHERITANCE
RULE NO.1:
• The sub class cannot inherit the following members of superclass
a) private
b) static
c) constructors
d) initialization Blocks
RULE NO.2:
• The super class cannot access the properties of the subclass.
RULE NO.3:
• If the class is final it cannot have subclass.

USES OF INHERITANCE
• Code Reusability.
• Software Enhancement.
• Code Modification.
• Generalization Vs. Specialization.
TYPES OF INHERITANCE
There are five types of Inheritance
1) Single Level Inheritance.
2) Multi Level Inheritance.
3) Hierarchical Inheritance.
4) Multiple Inheritance.
5) Hybrid Inheritance.
Example 3:

Example 4:
SINGLE LEVEL INHERITANCE
• In this form of Inheritance, the properties of a super class is derived by only one sub
class and that super class will have only one sub class.

B
Example:

MULTILEVEL INHERITANCE
• In this form of inheritance the properties of a super class is derived by more than one
sub class in different levels.

C
Example:
HIERARCHICAL INHERITANCE
• In this form of inheritance, the properties of a superclass is derived by more than one
sub class in same level.

B C

Example:
MULTIPLE INHERITANCE:
• In this form o inheritance a single sub class tries to directly inherit the properties of
more than one super class.
• This form of inheritance is illegal and not supports using classes.

A B

Jacob’s Diamond Ambiguity Problem:


• The subclass does not know from which super class it must acquire the properties of
class object.
Object

HYBRID INHERITANCE
• It is a combination of more than one form of inheritance.

A A

B C B

C D
D
COMPOSITION
• Composition is the concept of one class holding the object of another class.
• Composition is also known as [HAS-A Relationship].

Example 1:

Example 2:

Example 3:
Example 4:

You might also like