You are on page 1of 10

Basic Java

Non Primitive Data Type:-


• Array:-
An array is an non primitive datatype whose size is not fixed, An array is used to store multiple data of same data
type.
Array always store homogenous data.

Ways to declare array :-

1. int [] a ={5,4,6,7};
2. int a[] ={5,4,6,7};
3. int [] a; - declaration (This type of array can have infinite value.)
To assign value :- a = new int [] {1,2,3,4};

To declare an array with fixed size:-


• Int [] a = new int [20]; -(in this case we have declared an array with can only store 20 values of same data
types.)
Basic Concepts of Oops :-

•Object
•Class
•Inheritance
•Polymorphism
•Abstraction
•Encapsulation

1. Inheritance :-
Inheritance is an Oops concept where one class acquire the property of another class.
The class from which properties are acquired or inherited is called as Super Class/ Parent Class, and the class which
inherits or takes the property is called as Sub Class/Child Class. We always use “extends” keyword for inheritance.

We can also say that inheritance is a parent child relationship.

For better understanding lets refer the diagram:-


Without Inheritance
Super Class
/Parent Class
Class A {
Class A { Method1,
Method1, Method2,
Method2, Method3}
Method3}

Inheritance takes
place between A &
B

Class B{
Class B{ Method 1,
Method 4} Method 2,
Method 3,
Method 4}

Sub class /Child


class
For ex:-

Class A {

Public void test1() {


Syso(“Hello “)

Class B extends ClassA{

Public void test2(){


Syso(“Hello java”);

Public static void main(String [] args){


ClassB obj = new ClassB();
obj.test1; Here we see that test1 was
obj.test2; method of ClassA but we have
} called it with the object reference
of ClassB because Class B have
inherited all the property of
ClassA now
Types of inheritance:-
• Single Level Inheritance.
• Multi Level Inheritance.
• Multiple Inheritance.
• Hierarchical Inheritance/Hybrid Inheritance.

1.Single Level Inheritance :- When one class acquires the property of another class, or when a single sub class inherits the
property of a single super class this is known as Single level inheritance.

extends
Class A{ Class B{
} }
2.Multilevel Inheritance:-
When a subclass acquires the property of superclass and then this subclass is again inherited by another
subclass is known as multilevel Inheritance.

super class
Class{ m1, m2,
m3
ClassA

extends Super class

Class B
{ Class
extends c{M5,M4,m1,m2,
Level1 M4}
m3}
Sub class
ClassB{M4,m1,m2,m3}
Class C{
M5}
Level2
3. Multiple Inheritance : -
when one subclass acquires the property of multiple super class its called as multiple inheritance.

Object Class
Of java

Super Class Super Class

Class A{M1,M2} Class B{M3,M4}

Class C{M5}
M5,M1,m2,m3,m4

Sub Class
Multiple inheritance is not possible for class in Java due to diamond ambiguity problem.
Object is the super most class of java so in this case java gets confused that which route it has to take to call the supermost
object as Subclass points towards two super class
4. Hierarchical :-
When multiple sub class extends one super class, i.e. when more than one sub class inherits the property
of single super class , this is known as Hierarchical/Hybrid Inheritance.

Super Class

extends
Class A{m1,m2,m3} ClassD{m6}

ClassD{m6,m1,m2,m3
extends extends }

Class B{m4} Class C{m5}

ClassB{m4,m1,m2,m3} Sub class ClassC


{m5,m1,m2,m3}

ClassD{M7,m4,m1,
Class D{ m7}
m2,m3}
Class s

Class F

Class A

ClassD

Class B Class c

Class E

You might also like