Week4-OOPS Intro, Constructors, Inheritence
Week4-OOPS Intro, Constructors, Inheritence
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
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.
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
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();
}
}
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
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: