Yesterday was about primitive data type. Now its time to learn about non-primitive types.
user defined data types unlike primitive types and they can be easily created or modified by the users. They are used to store multiple values of either the same data type or different data types. (Ex: array)
Whenever non-primitive data type is defined, it gives reference to the memory location where the data is stored.
=> whenever non primitive data type is passed on to a function or a var, we are simply passing the address where the data is stored . (right part of the diagram is today's topic)
Example
class Demo{ //Class name - Demo having first inital letter in capital
int res = 100; //Class member variable
Demo(){ //Class default constructor
System.out.println(res);
}
public void add(int a,int b){ //Class member function
int c = a + b;
System.out.println("Addition of numbers: " + c);
}
public void sub(int a,int b){ //Class member function
int c = a - b;
System.out.println("Subtraction of numbers: " + c);
}
}
public class Main{
public static void main(String[] args) {
Demo obj = new Demo();
obj.add(10,20);
obj.sub(50,25);
}
}
Output
100
Addition of numbers: 30
Subtraction of numbers: 25
substring
public class SubStringExample {
public static void main(String[] args) {
String str = "Hello!";
String subStr = str.substring(0,3);
System.out.println(subStr);
}
}
the above code will output : "Hell"
Fully Abstract Class
/Interface declaration: by first user
interface Drawable{
void draw();
}
//Implementation: by second user
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class Circle implements Drawable{
public void draw(){System.out.println("drawing circle");}
}
//Using interface: by third user
class TestInterface1{
public static void main(String args[]){
Drawable d=new Circle();//In real scenario, object is provided by method e.g. getDrawable()
d.draw();
}}
Output
drawing circle